如何选择回收站视图中的项目并将项目位置设置为我的活动

时间:2016-05-12 05:31:14

标签: android android-recyclerview android-dialogfragment android-dialog recycler-adapter

我创建了一个自定义对话框片段,它包含一个Recyclerview和一个按钮,当我点击它选中的项目时,它应该突出显示,当点击按钮我想要特定项目位置或特定项目的数据到达我的主要活动。我尝试了不同的代码,但没有得到它,提到了不同的答案并尝试过。请帮帮我。

提前致谢。

DialogFragment:

 public class DiaFragment extends DialogFragment {

    RecyclerView recyclerView;
    public DiaFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_dialog, container, false);


        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
        recyclerView = (RecyclerView) v.findViewById(R.id.list);
        ListAdapter listAdapter = new ListAdapter(getContext(),getData());
        recyclerView.setAdapter(listAdapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());



        return v;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setPositiveButton("OK", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        return super.onCreateDialog(savedInstanceState);
    }

    public static List<Listen> getData()
    {
        List<Listen> data = new ArrayList<>();
        int[] images = {R.drawable.bottle,R.drawable.lotion,R.drawable.soap,R.drawable.soda,R.drawable.sodaa};
        String[] texts = {"250ml","300ml","500ml","750ml","1ltr"};
        for (int i=0;i<texts.length && i<images.length;i++){
            Listen current = new Listen();
            current.img = images[i];
            current.text= texts[i];
            data.add(current);
        }
        return data;
    }
}

recycleview adapter:

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



    private LayoutInflater inflater;
    List<Listen> data = Collections.emptyList();
    private int selectedPos = 0;

    public ListAdapter(Context context,List<Listen> data){
            inflater = LayoutInflater.from(context);
            this.data = data;
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.row, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Listen current = data.get(position);
        holder.text1.setText(current.text);
        holder.imge.setImageResource(current.img);
        holder.itemView.setSelected(selectedPos == position);
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView text1;
        public ImageView imge;
        public MyViewHolder(View itemView) {
            super(itemView);
            text1 = (TextView) itemView.findViewById(R.id.texty);
            imge = (ImageView) itemView.findViewById(R.id.ivy);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    notifyItemChanged(selectedPos);
                    selectedPos = getAdapterPosition();
                    notifyItemChanged(selectedPos);
                }
            });

        }

    }

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


}

1 个答案:

答案 0 :(得分:1)

在Dialog中,您可以调用getActivity()来访问启动Dialog的活动。然后通过使用您的活动类名称转换getActivity(),您可以访问受保护的方法并设置数据。

class MyActivity extends Activity {
    private Listen selectedListen;
    ...
    protected void setDataFromMyDialog(Listen listen) {
        this.selectedListen = listen;
    }
    ...
}

public class DiaFragment extends DialogFragment {

     ...
     protected ListAdapter listAdapter;
     ...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
      ...
      listAdapter = new ListAdapter(getContext(),getData());
      ...
 }

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setPositiveButton("OK", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

             ((MyActivity)getActivity()).setDataFromMyDialog(listAdapter.getSelectedData());
             dismiss(); // close dialog
        }
    });
    return super.onCreateDialog(savedInstanceState);
}

}


public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder> {
     ...
     public Listen getSelectedData() {
          return data.get(selectedPos);
     }
     ...
}