gpt4 book ai didi

java - Android:存储用户点击的CardView

转载 作者:行者123 更新时间:2023-11-29 19:09:08 26 4
gpt4 key购买 nike

我想创建一个用户点击过的 CardView 的列表。然后我想存储这些 CardView,然后通过电子邮件发送结果。下面是我的代码列表,如果您需要更多部分或有任何疑问,请在下面发布。谢谢。

回收 View 适配器:

    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();

}


}

RecyclerViewHolder:

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;

}
}

CarFrontFragment:(与 RecyclerView 结合使用的 fragment 示例)

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;
}

}

ItemObject:(对于 RecyclerView 中的每个项目)

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;}


}

理想情况下,我希望列表只输出项目名称(如果为真)。希望我们能够让它发挥作用。再次感谢。

最佳答案

有几种不同的方法可以将该信息从 ViewHolder 传递到 Adapter

查看this answer ,您可以看到一些选项,例如 EventBus 或使用 interface

然而,为了让它更直接、更容易,我只是移动了一个。如果您将 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

关于java - Android:存储用户点击的CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46019464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com