करने के लिए RecyclerView CardView क्लिक किया मद डाटा पासिंग मैं पूरी कहानी गतिविधि के लिए क्लिक किया cardview डेटा गुजर बारे में एक प्रश्न, और यहाँ है:गतिविधि
मैं एक गतिविधि "विवरण" कहा जाता है, जिसमें 2 TextViews शामिल है यह लेआउट है, शीर्षक & विवरण।
मैंने एक टुकड़ा (टैब_1) स्थापित किया है जिसमें रीसाइक्लिंगव्यू कोड और आइटम डेटा शामिल हैं, उनमें से प्रत्येक आइटम में शामिल हैं: शीर्षक & विवरण।
जो मैं चाहता:
उपयोगकर्ता आइटम क्लिक करते हैं, यह विवरण गतिविधि खुल जाएगा, और विवरण लेआउट का शीर्षक बदलना क्लिक किए गए आइटम शीर्षक के साथ, और विवरण के लिए एक ही।
मैंने दूसरी गतिविधि को उदाहरण के रूप में बनाने के लिए तैयार किया है, और इसे शुरू करने का इरादा बनाया है, साथ ही स्टैक ओवरफ्लो के लिए "addOnTouchlistener" धन्यवाद, मुझे इसे बनाने का तरीका मिला है।
तो, यह कैसे जीवित बनाना है? मैंने स्टैक ओवरफ्लो पर उपलब्ध उत्तरों के कई तरीकों की कोशिश की है, लेकिन वे सभी काम नहीं कर रहे हैं, या मेरे अनुरोध से संबंधित नहीं हैं।
यहाँ मेरी फ़ाइलें हैं:
itemsdata.java:
public class itemsdata {
int CatPic;
String title;
String Descr;
int Exapnd;
int expand_no;
tab_1.java (टुकड़ा)
public class tab_1 extends Fragment implements SearchView.OnQueryTextListener {
private RecyclerView mRecyclerView;
public RecyclingViewAdapter adapter;
private Activity context;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.tab_1, container, false);
mRecyclerView = (RecyclerView)layout.findViewById(R.id.recycler_view);
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener
(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent i = new Intent(view.getContext(), DetailsActivity.class);
view.getContext().startActivity(i);
}
}));
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new RecyclingViewAdapter(getActivity(),Listed());
mRecyclerView.setAdapter(adapter);
return layout;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextChange(String query) {
final List<itemsdata> filteredModelList = filter(Listed(), query);
adapter.animateTo(filteredModelList);
mRecyclerView.scrollToPosition(0);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
private List<itemsdata> filter(List<itemsdata> models, String query) {
query = query.toLowerCase();
final List<itemsdata> filteredModelList = new ArrayList<>();
for (itemsdata model : models) {
final String text = model.title.toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
public List<itemsdata> Listed()
{
//Titles Strings
String sys_title1 = getString(R.string.system_item_title_1);
String sys_title2 = getString(R.string.system_item_title_2);
String sys_title3 = getString(R.string.system_item_title_3);
//Description Strings
String sys_descr1 = getString(R.string.system_item_desc_1);
String sys_descr2 = getString(R.string.system_item_desc_2);
String sys_descr3 = getString(R.string.system_item_desc_3);
//Adding New Cards
List<itemsdata> data = new ArrayList<>();
//Categories Icons New Items ** Make It The Same
int[] icons = {
R.drawable.facebook_icon ,
R.drawable.twitter_icon ,
R.drawable.twitter_icon
};
//Expand Button New Items
int[] expandbutton = {
R.drawable.expanded ,
R.drawable.expanded ,
R.drawable.expanded
};
//UnExpand Button New Items
int[] unexpandbutton = {
R.drawable.ca_expand ,
R.drawable.ca_expand ,
R.drawable.ca_expand
};
//Titles New Items
String[] titles = {
sys_title1 ,
sys_title2 ,
sys_title3
};
//Description New Items
String[] Description = {
sys_descr1 ,
sys_descr2 ,
sys_descr3
};
for(int i = 0;i<titles.length && i < icons.length && i < Description.length && i < unexpandbutton.length && i < expandbutton.length ; i++)
{
itemsdata current = new itemsdata();
current.CatPic = icons[i];
current.title = titles[i];
current.Descr = Description[i];
current.expand_no = unexpandbutton[i];
current.Exapnd = expandbutton[i];
data.add(current);
}
return data;
}
}
विवरण गतिविधि:
public class DetailsActivity extends AppCompatActivity{
TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
title = (TextView)findViewById(R.id.details_title);
}
संपादित करें: मैं मैंने इसे बनाया है, मैंने एक जोड़ा है बटन जो खंड को खोलता है, और एडाप्टर में डेटा पास करता है, लेकिन मैं इसे tab_1.java के माध्यम से चाहता हूं, एडाप्टर नहीं, मेरा मतलब है कि मैं टुकड़े को खोलने के लिए आइटम पर क्लिक करना चाहता हूं, बटन पर नहीं, यहां एक स्नैप मेरे एडाप्टर कोड से (मैंने इसे ऑनबिंड व्यूहोल्डर में जोड़ा है)
मैंने ऑनक्लिक स्थापित किया है और Vew.setOnClick ..etc लागू किया है, लेकिन जब मैं आइटम पर क्लिक करता हूं, तो कुछ भी नहीं होता है।
@Override
public void onBindViewHolder(final MyRecycleViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),DetailsActivity.class);
v.getContext().startActivity(i);
}
});
//Referencing Data
final itemsdata currentobject = mdata.get(position);
//Referencing Items
holder.ProbTitle.setText(currentobject.title);
holder.ProbDescr.setText(currentobject.Descr);
holder.CategoryPic.setImageResource(currentobject.CatPic);
holder.ExpandButton.setImageResource(currentobject.Exapnd);
holder.ExpandNoButton.setImageResource(currentobject.expand_no);
//What Happen When You Click Expand Button .
holder.ExpandButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), DetailsActivity.class);
i.putExtra("TitleKey",holder.ProbTitle.getText().toString());
v.getContext().startActivity(i);
}
}
);
public static class MyRecycleViewHolder extends RecyclerView.ViewHolder
{
SwipeLayout swipeLayout;
//Defining Items .
TextView ProbTitle;
ImageButton ExpandButton;
TextView ProbDescr;
ImageButton ExpandNoButton;
ImageView CategoryPic;
/*
TextView Card_Star;
TextView Card_UnStar;
*/
TextView Card_Share;
//Referencing Resources
public MyRecycleViewHolder(final View itemView) {
super(itemView);
ProbTitle = (TextView) itemView.findViewById(R.id.prob_title);
CategoryPic = (ImageView) itemView.findViewById(R.id.cat_pic);
ProbDescr = (TextView) itemView.findViewById(R.id.prob_descr);
ExpandButton = (ImageButton) itemView.findViewById(R.id.expand_button);
ExpandNoButton = (ImageButton) itemView.findViewById(R.id.expand_no_button);
/*
Card_Star = (TextView) itemView.findViewById(R.id.card_star);
Card_UnStar = (TextView) itemView.findViewById(R.id.card_unstar);
*/
Card_Share = (TextView) itemView.findViewById(R.id.card_share);
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
}
संपादित की जांच करें, अपने तरीके से जीता काम नहीं करते हैं, उस स्ट्रिंग को पाने में समस्या, एडाप्टर से केवल संभावित तरीका है, लेकिन tab_1.java से, यह संभव नहीं है। – Jaeger
@AboHani RecyclerViewItemClickListener और IIememLongClickListener पर प्रदान नहीं करता है, आपको इसे केवल एडाप्टर में सेट करना चाहिए ... या आप इस 'सार्वजनिक इंटरफ़ेस OnItemClickLitener { void onItemClick (दृश्य देखें, int स्थिति) पर एडाप्टर में एक इंटरफ़ेस बना सकते हैं; void onItemLongClick (दृश्य देखें, int स्थिति); } 'और इसे कहते है जब प्रत्येक आइटम पर क्लिक करके या लंबे क्लिक किया है, इसलिए अब आप tab_1.java में एक श्रोता सेट कर सकते हैं। – chichiangho
हाँ, मैं इस तरह से पहली बार में जोड़ दिया है, लेकिन मैं डेटा पारित करने में सक्षम नहीं था, वैसे भी मैं इसे अनुकूलक में cardview संदर्भित और intent.putExtras का उपयोग कर डेटा गुजर के माध्यम से दिया है। बीटीडब्ल्यू, यदि आपके पास सर्चव्यू में ज्ञान है तो कृपया मेरे प्रोफाइल में मेरा प्रश्न देखें, और आपके उत्तर के लिए धन्यवाद :)। – Jaeger