从Bottomsheet为每个底部添加功能

时间:2016-11-27 17:20:47

标签: java android list bottom-sheet

我在列表视图中设置了不同的按钮: 这是我的应用程序:

http://imgur.com/a/dFSTF

MyBottomSheetDialogFragment:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sasank on 26-11-2016.
 */

public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {

    String mString;
    private RecyclerView mRecyclerView;
    private VerticalListAdapter mVerticalListAdapter;

    static MyBottomSheetDialogFragment newInstance(String string) {
        MyBottomSheetDialogFragment f = new MyBottomSheetDialogFragment();
        Bundle args = new Bundle();
        args.putString("string", string);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mString = getArguments().getString("string");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_sheet_modal_list, container, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        return v;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        List<Model> list = new ArrayList<Model>();
        list.add(new Model(R.drawable.ic_call_black_24dp, "Call"));
        list.add(new Model(R.drawable.ic_chat_black_24dp, "Chat"));
        list.add(new Model(R.drawable.ic_android_black_24dp, "Android"));
        list.add(new Model(R.drawable.ic_location_on_black_24dp, "Location"));
        list.add(new Model(R.drawable.ic_call_black_24dp, "Call"));
        list.add(new Model(R.drawable.ic_chat_black_24dp, "Chat"));
        list.add(new Model(R.drawable.ic_android_black_24dp, "Android"));
        list.add(new Model(R.drawable.ic_location_on_black_24dp, "Location"));


        mVerticalListAdapter = new VerticalListAdapter(getContext(), list);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mVerticalListAdapter);


    }
}

我的verticalListAdapter。

package com.example.user.bottomsheet_android;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.MyViewHolder>{

    private Context mContext;
    private List<Model> mModelList;

    public VerticalListAdapter(Context context, List<Model> list) {
        this.mContext = context;
        this.mModelList = list;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.modal_list_item, parent, false);

        return new MyViewHolder(itemView);


    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.txtView.setText(mModelList.get(position).getName());
        holder.imgView.setImageDrawable(mContext.getResources().getDrawable(mModelList.get(position).getImgId()));

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView txtView;
        public ImageView imgView;

        public MyViewHolder(View view) {
            super(view);
            txtView = (TextView) view.findViewById(R.id.txtView);
            imgView = (ImageView) view.findViewById(R.id.imgView);





        }
    }


}

我的目标是为底部工作表中显示的每个按钮添加功能。

如有任何澄清,请发表评论。

1 个答案:

答案 0 :(得分:0)

在你的onBindViewHolder方法中,我会放一个onClickListener,然后使用switch-case结构或几个不同的if-elseif语句。例如,switch-case看起来像:

holder.txtView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        switch (holder.getAdapterPosition()) {
            case 1:
                // do call related things here
                .
                .
                .
                break;
            case 2:
                // do chat related things here
                .
                .
                .
                break;

        }
    }
}

等等。您可以使用if语句实现类似的东西