RecyclerView单击更改项目布局

时间:2016-01-02 19:49:55

标签: android layout draw

我有像活动这样的列表视图:

enter image description here

我想点击这样更改项目布局:

enter image description here

我的RecycleView适配器:       公共类ComenziViewAdapter扩展了RecyclerView.Adapter {

private final List<Comanda> mValues;
private final ComenziFragment.OnListFragmentInteractionListener listener;
public ComenziViewAdapter(List<Comanda> items, ComenziFragment.OnListFragmentInteractionListener listener) {
    mValues = items;
    this.listener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_comenzi, parent, false);
    View expandedView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_comenzi_expanded,parent,false);

    return new ViewHolder(view,expandedView);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.mItem = mValues.get(position);
    holder.mIdView.setText(""+mValues.get(position).getId());
    holder.mContentView.setText(mValues.get(position).getTraseu());

    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.setView(holder.getExpandedView());

            listener.onListFragmentInteraction(holder.mItem);
        }
    });
}

@Override
public int getItemCount() {
    return mValues.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public View mView;
    public View expandedView;
    public TextView mIdView;
    public TextView mContentView;
    public Comanda mItem;

    public ViewHolder(View view,View expandedView) {
        super(view);
        mView = view;
        this.expandedView = expandedView;
        mIdView = (TextView) view.findViewById(R.id.id);
        mContentView = (TextView) view.findViewById(R.id.content);
    }
    public View getView()
    {
        return this.mView;
    }
    public void setView(View view)
    {
        mView = view;
    }
    public View getExpandedView()
    {
        return this.expandedView;
    }
    @Override
    public String toString() {
        return super.toString() + " '" + mContentView.getText() + "'";
    }
}

}

如何在用户点击项目时更改布局?

2 个答案:

答案 0 :(得分:1)

我认为这不是正确的做法(更改ViewHolder setView)

简单的解决方案是在R.layout.fragment_comenzi中使“扩展”视图消失,并在点击注册时使其可见。

答案 1 :(得分:0)

我的解决方案是:

  1. 在布局文件中,我添加了第二个具有INVISIBLE状态的LinearLayout。
  2. ViewHolder有一个布尔属性状态,表示列表项状态(false - 未展开(第二个LinearLayout隐藏),true - 展开(第二个LinearLayout可见))。
  3. 更改了ClickListener:

    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayout expandedLayout = (LinearLayout) holder.mView.findViewById(R.id.comenzi_expanded_layout);
    
            expandedLayout.setVisibility(!holder.state ? View.VISIBLE : View.GONE);
            holder.state = !holder.state;
            listener.onListFragmentInteraction(holder.mItem);
        }
    });