扩展时,将所有其他回收库中的项目崩溃



我有一个可稳定的可扩展回收库。单击任何项目时,它将扩展以显示更多详细信息。但是我想在这里改变。单击一个项目进行展开时,所有其他项目都应折叠。这是我的适配器代码:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private ArrayList<ListItem> android_versions;
    private List<ListItem> listItems;
    private LayoutInflater layoutInflater;
    private Animation animationUp, animationDown;
    private Context context;
    private final int COUNTDOWN_RUNNING_TIME = 500;
    int i =0;
    public DataAdapter(Context context, java.util.List<ListItem> android_versions,
                       Animation animationUp, Animation animationDown) {
        this.context = context;
        this.listItems = android_versions;
        this.animationDown = animationDown;
        this.animationUp = animationUp;
    }
    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
        return new ViewHolder(view);
    }
    public String getRandomColor(int a){
        String colors[] = {"#ef5350","#f44336","#ef5350"};
        return colors[a];
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, int i) {
        ListItem listItem = listItems.get(i);
        if (i%2==2)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(0)));
        else if (i%2==1)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(1)));
        else if (i%2==0)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(2)));
        holder.tv_android.setText(listItem.getSubName());
        holder.contentLayout.setText(listItem.getContent());
        holder.expandit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.contentLayout.isShown()) {
                    holder.contentLayout.startAnimation(animationUp);
                    CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                        }
                        @Override
                        public void onFinish() {
                            holder.contentLayout.setVisibility(View.GONE);
                        }
                    };
                    countDownTimerStatic.start();
                } else {
                    holder.contentLayout.setVisibility(View.VISIBLE);
                    holder.contentLayout.startAnimation(animationDown);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return listItems.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_android;
        private TextView contentLayout;
        RelativeLayout expandit;
        public ViewHolder(View view) {
            super(view);
            tv_android = (TextView)view.findViewById(R.id.tv_android);
            contentLayout = (TextView) view.findViewById(R.id.content);
            expandit = (RelativeLayout)view.findViewById(R.id.expandit);
        }
    }
}

我认为不可能按位置更改视图持有人的内容。因此,如何按位置隐藏所有其他项目的内容。我认为不需要其他代码,所以我还没有包含它。

编辑:Ashish建议之后的代码:

  private void changeStateOfItemsInLayout(ListItem listItem) {
        for (int i = 0; i < listItems.size(); i++) {
            if (listItems.get(i) == listItem)
                listItem.setShouldBeExpanded(true);
            else
                listItem.setShouldBeExpanded(false);
        }
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, int i) {
        ListItem listItem = listItems.get(i);
        if (i%2==2)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(0)));
        else if (i%2==1)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(1)));
        else if (i%2==0)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(2)));
        holder.tv_android.setText(listItem.getSubName());
        holder.contentLayout.setText(listItem.getContent());
        if(listItem.getShouldBeExpanded()){
            holder.contentLayout.setVisibility(View.VISIBLE);
            holder.contentLayout.startAnimation(animationDown);
        }else{
            holder.contentLayout.startAnimation(animationUp);
            CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
                @Override
                public void onTick(long millisUntilFinished) {
                }
                @Override
                public void onFinish() {
                    holder.contentLayout.setVisibility(View.GONE);
                }
            };
            countDownTimerStatic.start();
        }
        holder.expandit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.contentLayout.isShown()) {
                    holder.contentLayout.startAnimation(animationUp);
                    CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                        }
                        @Override
                        public void onFinish() {
                            holder.contentLayout.setVisibility(View.GONE);
                        }
                    };
                    countDownTimerStatic.start();
                } else {
                    holder.contentLayout.setVisibility(View.VISIBLE);
                    holder.contentLayout.startAnimation(animationDown);
                }
                listItems.get(holder.getLayoutPosition()).setShouldBeExpanded(true);
                changeStateOfItemsInLayout(listItems.get(holder.getLayoutPosition()));
                notifyDataSetChanged();
            }
        });
    }

尽管在OnClick中,我仍然有扩展/倒塌布局的代码,即使我删除了它,它也行不通。

如果需要,这是我的模态类:

public class ListItem {
    private String subName;
    private String content;
    private String param;
    private Boolean shouldBeExpanded=false;
    public Boolean getShouldBeExpanded() {
        return shouldBeExpanded;
    }
    public void setShouldBeExpanded(Boolean shouldBeExpanded) {
        this.shouldBeExpanded = shouldBeExpanded;
    }
    public ListItem(String subName, String content) {
        this.subName = subName;
        this.content = content;
        this.param = "Nothing";
    }
    public String getParam() {
        return param;
    }
    public ListItem(String subName, String content, String param) {
        this.subName = subName;
        this.content = content;
        this.param = param;
    }
    public String getSubName() {
        return subName;
    }
    public String getContent() {
        return content;
    }
}

编辑:,根据要求,这是动画文件和AnimationDown文件的代码:

animationup.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >
    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="0.0" />
</set>

animationDown.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

多亏了Ashish,我用简单的逻辑解决了问题。这是我重新定义的 ChangestateOfitemsInlayout 功能代码:

private void changeStateOfItemsInLayout(ListItem listItem, int p) {
        for (int x = 0; x < listItems.size(); x++) {
            if (x == p) {
                listItem.setShouldBeExpanded(true);
                //Since this is the tapped item, we will skip
                //the rest of loop for this item and set it expanded
                continue;
            }
            listItems.get(x).setShouldBeExpanded(false);
        }
    }

此功能负责折叠所有其他项目。它检查了哪个选项并将其扩展(如果已经扩展,我们不会从OnBindViewHolder函数调用此函数(并折叠所有其他。

这是我的完整适配器代码:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private ArrayList<ListItem> android_versions;
    private List<ListItem> listItems;
    private LayoutInflater layoutInflater;
    private Animation animationUp, animationDown;
    private Context context;
    private final int COUNTDOWN_RUNNING_TIME = 300;
    public DataAdapter(Context context, java.util.List<ListItem> android_versions,
                       Animation animationUp, Animation animationDown) {
        this.context = context;
        this.listItems = android_versions;
        this.animationDown = animationDown;
        this.animationUp = animationUp;
    }
    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
        return new ViewHolder(view);
    }
    public String getRandomColor(int a){
        String colors[] = {"#ef5350","#f44336","#ef5350"};
        return colors[a];
    }
    private void changeStateOfItemsInLayout(ListItem listItem, int p) {
        for (int x = 0; x < listItems.size(); x++) {
            if (x == p) {
                listItem.setShouldBeExpanded(true);
                //Since this is the tapped item, we will skip
                //the rest of loop for this item and set it expanded
                continue;
            }
            listItems.get(x).setShouldBeExpanded(false);
        }
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int i) {
        ListItem listItem = listItems.get(i);
        if (i%2==2)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(0)));
        else if (i%2==1)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(1)));
        else if (i%2==0)
            holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(2)));
        holder.tv_android.setText(listItem.getSubName());
        holder.contentLayout.setText(listItem.getContent());
        if(listItem.getShouldBeExpanded()){
            holder.contentLayout.setVisibility(View.VISIBLE);
            holder.contentLayout.startAnimation(animationDown);
        }else{
            holder.contentLayout.startAnimation(animationUp);
            CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
                @Override
                public void onTick(long millisUntilFinished) {}
                @Override
                public void onFinish() {
                    holder.contentLayout.setVisibility(View.GONE);
                }
            };
            countDownTimerStatic.start();
        }
        holder.expandit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.contentLayout.isShown()) {
                    listItems.get(holder.getLayoutPosition()).setShouldBeExpanded(false);
                } else {
                    listItems.get(holder.getLayoutPosition()).setShouldBeExpanded(true);
                    changeStateOfItemsInLayout(listItems.get(holder.getLayoutPosition()),holder.getLayoutPosition());
                }
                notifyDataSetChanged();
            }
        });
    }
    @Override
    public int getItemCount() {
        return listItems.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_android;
        private TextView contentLayout;
        RelativeLayout expandit;
        public ViewHolder(View view) {
            super(view);
            tv_android = (TextView)view.findViewById(R.id.tv_android);
            contentLayout = (TextView) view.findViewById(R.id.content);
            expandit = (RelativeLayout)view.findViewById(R.id.expandit);
        }
    }
}

在您的ListItem类中,我假设是模型类,添加一个新的布尔值shore shorgexpanded,并将其默认值设置为false,然后编写其getter and setter。现在在您的适配器中,在OnBindViewHolder中编写以下代码

if(listItem.shouldBeExpanded()){
//write your code to expand it
}else{
//write the code to collapse
}

现在在"代码"部分中,在一些扩展布局的onclick中,设置了此变量,以使该lisitem为true,并调用将ListItem中的所有其他项目设置为false forse forse forse forse shose shosebeexpanded for shose shosexpanded ..某些whits以下函数。ChangestateOfitemsInlayOut(ListItem(。现在称为notifiydatasetchanged((。

private void changeStateOfItemsInLayout(ListItem listItem){
for(int i=0;i<listItems.size();i++){
if(listItems.get(i)==listItem)
listItem.get(i).setShouldBeExpanded(true);
}else{
listItem.get(i).setShouldBeExpanded(false);
}
}

这应该解决您的目的。

我花了几个小时来摆弄explablerecycleradapter代码。但这真的很简单。在您的活动中,您有用于展开/崩溃事件的听众,当前的父列表项目位置正在扩展/崩溃。跟踪当前的expandedPosition,并调用适配器崩溃的折叠量以崩溃先前扩展的项目。5行代码中的完美手风琴效果。

private currentExpandedPosition = -1;
        mAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
        @Override
        public void onListItemExpanded(int position) {
            if (currentExpandedPosition >= 0){
                mAdapter.collapseParent(currentExpandedPosition);
            }
            currentExpandedPosition = position;
        }
        @Override
        public void onListItemCollapsed(int position) {
           currentExpandedPosition = -1;
        }
    });

相关内容

  • 没有找到相关文章

最新更新