在recyclerview内部更改视图

时间:2016-09-01 05:32:11

标签: android android-recyclerview

我已经制作了一个应用程序,我必须在印度列车中显示可用的旅行课程.... the expected output图片显示了可用类的外观......

每列火车都有不同的旅行班级.. 我使用了六种不同的文本视图来显示可用的类

现在的问题是......一旦我滚动可用的类更改并变得不正确...... notice the first and second rows the available classes gets changed

行未正确更新,因为视图持有者会回收视图...但如何更正

这是适配器代码

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> {

ClassType[] classTypes;
MyRecyclerAdapter.CustomViewHolder holder;
private List<Train> trainList;
private Context mContext;

public MyRecyclerAdapter(Context context, List<Train> feedItemList) {
    this.trainList = feedItemList;
    this.mContext = context;
}

@Override
public MyRecyclerAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, null);
    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(final MyRecyclerAdapter.CustomViewHolder holder, int position) {
    this.holder = holder;
    final Train train = trainList.get(position);

    holder.expandableLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (holder.imageView.getTag().toString().equals("down")) {
                holder.imageView.setImageResource(R.drawable.uparrow);
                holder.imageView.setTag("up");
            } else {
                holder.imageView.setImageResource(R.drawable.downarrow);
                holder.imageView.setTag("down");
            }
        }
    });

    holder.trainNumber.setText(train.getNumber());
    holder.trainName.setText(train.getName());
    holder.from.setText(train.getFrom().getCode());
    holder.to.setText(train.getTo().getCode());
    holder.srcTime.setText(train.getSrc_departure_time());
    holder.destTime.setText(train.getDest_arrival_time());
    classTypes = train.getClasses();

    for (ClassType classType : classTypes) {
        switch (classType.getClass_code()) {
            case "1A":
                if (!classType.getAvailable().equals("Y"))
                    holder.firstac.setVisibility(View.GONE);
                break;
            case "2A":
                if (!classType.getAvailable().equals("Y"))
                    holder.secondac.setVisibility(View.GONE);
                break;
            case "3A":
                if (!classType.getAvailable().equals("Y"))
                    holder.thirdac.setVisibility(View.GONE);
                break;
            case "SL":
                if (!classType.getAvailable().equals("Y"))
                    holder.sleeper.setVisibility(View.GONE);
                break;
            case "2S":
                if (!classType.getAvailable().equals("Y"))
                    holder.secondseating.setVisibility(View.GONE);
                break;
            case "CC":
                if (!classType.getAvailable().equals("Y"))
                    holder.chaircar.setVisibility(View.GONE);
                break;
        }
    }
}

@Override
public int getItemCount() {
    return (null != trainList ? trainList.size() : 0);
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    protected TextView trainNumber, trainName, from, srcTime, to, destTime, firstac, secondac, thirdac, sleeper, secondseating, chaircar;
    protected ExpandableLayout expandableLayout;
    protected ImageView imageView;
    protected LinearLayout availableClasses;

    public CustomViewHolder(View view) {
        super(view);
        this.trainNumber = (TextView) view.findViewById(R.id.trainNumber);
        this.expandableLayout = (ExpandableLayout) view.findViewById(R.id.expand);
        this.trainName = (TextView) view.findViewById(R.id.trainName);
        this.from = (TextView) view.findViewById(R.id.from);
        this.srcTime = (TextView) view.findViewById(R.id.sourceTime);
        this.to = (TextView) view.findViewById(R.id.to);
        this.destTime = (TextView) view.findViewById(R.id.destTime);
        this.imageView = (ImageView) view.findViewById(R.id.arrow);
        this.availableClasses = (LinearLayout) view.findViewById(R.id.availableClasses);
        this.firstac = (TextView) view.findViewById(R.id.firstac);
        this.secondac = (TextView) view.findViewById(R.id.secondac);
        this.thirdac = (TextView) view.findViewById(R.id.thirdac);
        this.sleeper = (TextView) view.findViewById(R.id.sleeper);
        this.secondseating = (TextView) view.findViewById(R.id.secondseating);
        this.chaircar = (TextView) view.findViewById(R.id.chaircar);
    }
}

}

请帮助

1 个答案:

答案 0 :(得分:0)

首先:通过删除变量holder 的成员类声明,您对引用holder开始有些含糊不清

MyRecyclerAdapter.CustomViewHolder holder;

你不需要这个,这可能会在你或者适配器类处理视图时产生一些混合。从这开始,看看你得到了什么。

第二:您的代码仅与可见性相关隐藏视图,这将创建视图隐藏在某个列表项上时的错误,即使它对其他项目应该是可见的,你不是这样做的。

所以你应该首先显示所有相关的视图(重置),然后执行if-else并隐藏应隐藏的内容。

在for循环开始之前添加它

holder.firstac.setVisibility(View.VISIBLE);
holder.secondac.setVisibility(View.VISIBLE);
holder.thirdac.setVisibility(View.VISIBLE);
holder.sleeper.setVisibility(View.VISIBLE);
holder.secondseating.setVisibility(View.VISIBLE);
holder.chaircar.setVisibility(View.VISIBLE);

for (ClassType classType : classTypes) {
:
: