Recycviewview中的Listview和按钮

时间:2015-12-19 07:39:44

标签: android listview android-arrayadapter android-recyclerview android-cardview

我有一个Recyclerview,里面有一些卡片。每张卡片里面都有一个按钮和一个列表视图。我希望listview仅在单击按钮时出现。最初我将onClickListener设置为button而onTouchListener设置为recyclerview,但它不适用于按钮。它只识别了recyclerview监听器,而不是按钮单击监听器。如何独立设置两个监听器?这样,recyclerview touchlistener不会干扰按钮点击监听器。

其次,我在这里有两个适配器 - 一个用于listview,一个用于recyclerview。 Listview适配器在recyclerview适配器内部调用。如果我在单击按钮时调用listview适配器,则找不到我最初在recyclerview适配器中传递的上下文。

这是recyclerview适配器:

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder>{

static Context activity;
ArrayList<ChatModel> chats = new ArrayList<ChatModel>();
boolean visible = false;

public ChatAdapter(Context a,ArrayList<ChatModel> _chats){
    activity = a;
    chats = _chats;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
    ImageButton expand;
    ListView comments;
    public ViewHolder(View v) {
        super(v);
        expand = (ImageButton)v.findViewById(R.id.chat_open);
        comments = (ListView)v.findViewById(R.id.commentList);
    }
}

@Override
public int getItemCount() {
    // TODO Auto-generated method stub
    if(chats.size()<=0)
        return 0;
    return chats.size();
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    // TODO Auto-generated method stub
    try{

        holder.comments.setVisibility(View.GONE);
        holder.expand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG","Expand clicked");
                if (visible) {
                    visible = false;
                    holder.comments.setVisibility(View.GONE);
                } else {
                    visible = true;
                    CommentsAdapter adap = new CommentsAdapter(activity,                    chats.get(position).getCommentList());
                    holder.comments.setAdapter(adap);
                    holder.comments.setVisibility(View.VISIBLE);
                }
            }
        });

    }catch(Exception e){
        e.printStackTrace();
    }

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
    // TODO Auto-generated method stub
    View v = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.chat_item_card, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;

}
}

这是listview适配器:

public class CommentsAdapter extends ArrayAdapter<CommentModel>{
public Context context;
ArrayList<CommentModel> data;
CommentModel detail;

public CommentsAdapter(Context a, ArrayList<CommentModel> d) {
    super(a, R.layout.comment_item, d);
    context = a;
    data=d;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    detail = data.get(position);
    View vi = inflater.inflate(R.layout.comment_item, parent, false);
    TextView comment = (TextView) vi.findViewById(R.id.comment);

    comment.setText(detail.getComment());

    return vi;
}
}

这是cardview布局(recyclelerview行):

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    android:background="#e6e6e6"
    card_view:cardCornerRadius="5dp"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#272626">
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@drawable/expand"
                    android:id="@+id/chat_open"
                    android:layout_marginRight="4dp"
                    android:padding="2dp"
                    android:clickable="true"/>
        <ListView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_below="@+id/chat_open"
            android:layout_marginTop="5dp"
            android:layout_alignLeft="@+id/chat_open"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="3dp"
            android:id="@+id/commentList">

        </ListView>
    </RelativeLayout>

</android.support.v7.widget.CardView>

0 个答案:

没有答案