我想创建一个用户点击的CardView列表。然后,我想存储这些CardView,然后通过电子邮件发送结果。以下是我的代码列表,如果您需要更多部件或有疑问,请在下面发布。谢谢。
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
//holder.countryName.setText(itemList.get(position).getName());
holder.photo.setImageResource(itemList.get(position).getPhoto());
holder.view = itemList.get(position).getView();
}
@Override
public int getItemCount() {
Log.e("Test", "" + this.itemList.size());
return this.itemList.size();
}
}
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnTouchListener{
//public TextView countryName;
public ImageView photo;
public ImageView fbumper;
public View view;
public RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnTouchListener(this);
photo = (ImageView)itemView.findViewById(R.id.cardview);
//selected = false;
}
@Override
public boolean onTouch(View view1, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
view.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_POINTER_UP:
view.setVisibility(View.GONE);
break;
}
return true;
}
}
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.support.annotation.Nullable;
import java.util.List;
import java.util.ArrayList;
public class CarFrontFragment extends Fragment implements View.OnTouchListener {
// Do not use static views.
//Declaring variables/LayoutManager
private LinearLayoutManager Layout;
public ImageView bumper;
public ImageView hood;
@Nullable
@Override
//Making the fragment inflate (display) the view I need
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_car_front, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Bind your views. (Setting my variables equal to what they need to be)
RecyclerView list = (RecyclerView) view.findViewById(R.id.front_recyclerview);
bumper = (ImageView) view.findViewById(R.id.fbumperselected);
hood = (ImageView) view.findViewById(R.id.hoodselected);
// Setting the onTouch to hide/show the bumper.
view.setOnTouchListener(this);
// Create your layout manager.
Layout = new LinearLayoutManager(getContext());
list.setLayoutManager(Layout);
// Fetch your items.
List<ItemObject> rowListItem = getAllItemList();
// Set your adapter
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), rowListItem);
list.setAdapter(adapter);
}
// Adding my items (CardViews) to the list to be displayed.
private List<ItemObject> getAllItemList()
{
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject("Bumper", R.drawable.fbumper_card, bumper, false));
allItems.add(new ItemObject("Hood", R.drawable.hood_card, hood, false));
return allItems;
}
// Creating a new Fragment through newInstance.
public static CarFrontFragment newInstance() {
return new CarFrontFragment();
}
//Setting onTouch Events
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
}
import android.view.View;
public class ItemObject {
private String name;
private int photo;
private View view;
private Boolean selected;
public ItemObject(String name, int photo, View view, boolean selected) {
this.name = name;
this.photo = photo;
this.view = view;
this.selected = selected;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
public View getView() { return view; }
public Boolean getSelected() {return selected;}
}
理想情况下,我希望列表只输出项目的名称,如果它是真的。希望我们能够实现这一目标。再次感谢。
答案 0 :(得分:2)
有几种不同的方法可以将这些信息从ViewHolder
传递到Adapter
。
查看this answer,您可以看到一些选项,例如EventBus
或使用interface
。
然而,为了让它变得更加直接和简单,我只是去了class
。如果您将RecyclerViewHolders
移至RecyclerViewAdapter
课程内,则可以访问itemList
。
class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.photo.setImageResource(itemList.get(position).getPhoto());
holder.view = itemList.get(position).getView();
}
@Override
public int getItemCount() {
return itemList.size();
}
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnTouchListener{
ImageView photo;
public View view;
RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnTouchListener(this);
photo = itemView.findViewById(R.id.cardview);
}
@Override
public boolean onTouch(View view1, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
view.setVisibility(View.VISIBLE);
itemList.get(getAdapterPosition()).setSelected(true);
break;
case MotionEvent.ACTION_POINTER_UP:
view.setVisibility(View.GONE);
itemList.get(getAdapterPosition()).setSelected(false);
break;
}
return true;
}
}
}
只要这些类不太复杂,这应该是一个非常简单的解决方案。如果这不起作用,使用interface
将是我的下一个建议。