我想在用户点击dialog
中的特定项目时显示recycler view
。现在我不知道如何做到这一点,但我正在考虑使用我的主要活动中的interface
。 adapter
会将项目(TextView
文本)传递到界面中,然后主要活动将接受参数项并显示基于它的对话框。但我不知道如何做到这一点。或者,如果有任何替代方式
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter {
private List<String> mDataSet = new ArrayList<>();
private LayoutInflater mInflater;
private final ViewBinderHelper binderHelper = new ViewBinderHelper();
public RecyclerAdapter(Context context, List<String> dataSet) {
mDataSet = dataSet;
mInflater = LayoutInflater.from(context);
// uncomment if you want to open only one row at a time
// binderHelper.setOpenOnlyOne(true);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.row_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
final ViewHolder holder = (ViewHolder) h;
if (mDataSet != null && 0 <= position && position < mDataSet.size()) {
final String data = mDataSet.get(position);
// Use ViewBindHelper to restore and save the open/close state of the SwipeRevealView
// put an unique string id as value, can be any string which uniquely define the data
binderHelper.bind(holder.swipeLayout, data);
// Bind your data here
holder.bind(data);
}
}
@Override
public int getItemCount() {
if (mDataSet == null)
return 0;
return mDataSet.size();
}
/**
* Only if you need to restore open/close state when the orientation is changed.
* Call this method in {@link android.app.Activity#onSaveInstanceState(Bundle)}
*/
public void saveStates(Bundle outState) {
binderHelper.saveStates(outState);
}
/**
* Only if you need to restore open/close state when the orientation is changed.
* Call this method in {@link android.app.Activity#onRestoreInstanceState(Bundle)}
*/
public void restoreStates(Bundle inState) {
binderHelper.restoreStates(inState);
}
private class ViewHolder extends RecyclerView.ViewHolder {
private SwipeRevealLayout swipeLayout;
private View tickOffLayout;
private View infoLayout;
private View displayedText;
private TextView textView;
private CheckBox checkBox;
public ViewHolder(View itemView) {
super(itemView);
swipeLayout = (SwipeRevealLayout) itemView.findViewById(R.id.swipe_layout);
tickOffLayout = itemView.findViewById(R.id.tickOff_layout);
infoLayout = itemView.findViewById(R.id.info_layout);
displayedText = itemView.findViewById(R.id.displayedItemOnRecyclerView);
textView = (TextView) itemView.findViewById(R.id.text);
checkBox = (CheckBox) itemView.findViewById(R.id.checkItem);
}
public void bind(String data) {
/* tickOffLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkBox.isChecked()){
Log.e("Checkbox", "IS CHECKED");
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
tickOffLayout.setBackgroundColor(Color.parseColor("#52abff"));
notifyDataSetChanged();
}else if(!checkBox.isChecked()){
Log.e("Checkbox", "IS UNCHECKED");
textView.setPaintFlags(textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
tickOffLayout.setBackgroundColor(Color.parseColor("#d9d9d9"));
notifyDataSetChanged();
}
}
});*/
infoLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notifyDataSetChanged();
}
});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(checkBox.isChecked()){
Log.e("Checkbox", "IS CHECKED");
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
tickOffLayout.setBackgroundColor(Color.parseColor("#52abff"));
displayedText.setBackgroundColor(Color.parseColor("#CCCCCC"));
notifyDataSetChanged();
}else if(!checkBox.isChecked()){
Log.e("Checkbox", "IS UNCHECKED");
textView.setPaintFlags(textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
tickOffLayout.setBackgroundColor(Color.parseColor("#d9d9d9"));
displayedText.setBackgroundResource(R.drawable.define_background);
notifyDataSetChanged();
}
}
});
textView.setText(data);
}
}
}
答案 0 :(得分:0)
以下是如何以正确的方式设置ViewHolder和ViewAdapter,并在点击时使用DialogFragment节目处理onClick:
private class SimpleTitleDataHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
/**
* View's elements
*/
private TextView mTitleTV;
// Good object instance
private String mTitle;
// Constructor
public SimpleTitleDataHolder(View itemView) {
super(itemView);
/**
* Set elements
*/
mTitleTV = (TextView)
itemView.findViewById(R.id.title);
}
@Override
public void onClick(View v) {
// Do anything
// Show dialog
DialogFragment dialogFragment = new DialogFragment();
FragmentManager fm = getFragmentManager();
dialogFragment.show(fm, "tag");
}
/**
* Function to update view's elements
* @param user Good data to be updated to
*/
public void bindData(String title) {
mData = title;
mUserName.setText(title);
}
}
private class ActiveAuctionsDataAdapter extends RecyclerView.Adapter<BaseAuctionUserDataHolder> {
// Data list
private List<String> mDataList;
/**
* Constructor, which takes list of data and assigns it to local variable
* @param dataList
*/
public ActiveAuctionsDataAdapter(List<String> dataList) {
mDataList = dataList;
}
/**
* Define behaviour on creation
* @param parent Parent viewgroup
* @param viewType Type of view
* @return holder
*/
@Override
public BaseAuctionUserDataHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
switch (viewType) {
case 1:
View itemView1 = inflater.inflate(R.layout.simple_title, parent, false);
return new SimpleTitleDataHolder(itemView1);
}
return null;
}
/**
* Event that happens on cell append
* @param holder Holder to append to
* @param position Position of the cell
*/
@Override
public void onBindViewHolder(BaseAuctionUserDataHolder holder, int position) {
User data = mDataList.get(position);
holder.bindData(data);
}
@Override
public int getItemViewType(int position) {
/**
* Here you can dynamically determine what layout to inflate for each element of a RCV
*/
return 1;
}
/**
* Function to get number of items in list
* @return Number of items
*/
@Override
public int getItemCount() {
return mDataList.size();
}
}