1
我已經創建了一些代碼,它從數據庫中提取數據,然後填充cardView。 cardView行有一個隱藏的(不見的)textView,我想在單擊卡時啓用,因此展開視圖。 所有這些似乎工作正常,但由於某種原因,它不僅擴展了項目1,而且還擴展了另一個項目,我明顯不希望看到擴展的原因。Recycleview擴展多個iterms
這裏是我的適配器
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.blackout.kfzquiz.model.Product;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Sundown on 01/10/2016.
*/
public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.ViewHolder> {
private String[] kennzeichen_array;
private String[] stadt_array;
private int length;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder{
//each data item is just a string in this case
public TextView kennzeichen;
public TextView hiddenTextView;
public ViewHolder(View v){
super(v);
kennzeichen = (TextView) v.findViewById(R.id.tv);
hiddenTextView = (TextView) v.findViewById(R.id.textView8);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter2(List<Product> myItems){
length = myItems.size();
String[] mStrings = new String[myItems.size()];
String[] stadtStrings = new String[myItems.size()];
for(int i=0;i<myItems.size();i++) {
mStrings[i] = myItems.get(i).getkfz();
stadtStrings[i] = myItems.get(i).getStadt();
}
kennzeichen_array = mStrings;
stadt_array = stadtStrings;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter2.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
LayoutInflater inflater = LayoutInflater.from(
parent.getContext());
View v =
inflater.inflate(R.layout.my_text_view, parent, false);
// set the view's size, margins, paddings and layout parameters
MyAdapter2.ViewHolder vh = new MyAdapter2.ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final MyAdapter2.ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.kennzeichen.setText(kennzeichen_array[position]);
holder.hiddenTextView.setText(stadt_array[position]);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Clicked", String.valueOf(holder.getAdapterPosition()));
if (holder.hiddenTextView.getVisibility() == View.VISIBLE){
holder.hiddenTextView.setVisibility(View.GONE);
}
else{
holder.hiddenTextView.setVisibility(View.VISIBLE);
}
}
});
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return length;
}
}
而且一些截圖提前
感謝
天才隊友,感謝您的快速和有益的答案!甚至沒有想過這樣做。似乎挺直。 – T1M