使用 RecyclerView 制作基于客户列表适配器的水平列表视图 Android
use a RecyclerView to make a horizontal list view based off a customer list adapter Android
我正在尝试制作图片的水平滚动视图,并在每张图片上附加一个 OnClick 侦听器,将用户发送到新网页。我已经很好地处理了垂直列表,但现在我希望它是水平的。我怎样才能做到这一点。
我试过使用像 Android 建议的 recyclerView,但我无法让它接受适配器。
它给我这个错误:
回收器视图不能应用于 CustomeListAdpater,以及当我尝试添加 onClick 时它说它无法解析方法 setOnClickListern(anonymous android.widget.AdapterView.OnItemClickListener())
这是我目前所掌握的;
这里是线性布局管理器
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
recyclerView = (RecyclerView)findViewById(R.id.promotionHolder);
recyclerView.setLayoutManager(layoutManager);
这里是我将客户列表适配器分配给回收视图的地方
private void fillListView(ArrayList<ListItem> listData)
{
final ListView listView = (ListView) findViewById(R.id.custom_list);
listView.setAdapter(new CustomListAdapter(this, listData));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//here is a method to either send the user to the poduct page
//or to the main page in the app
//open a new activity and close this one down
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem promoData = (ListItem) listView.getItemAtPosition(position);
//lest open up the corrisponding webpage
Intent reDirect = new Intent();
reDirect.setAction(Intent.ACTION_VIEW);
reDirect.addCategory(Intent.CATEGORY_BROWSABLE);
reDirect.setData(Uri.parse(promoData.getPathUrl()));
String newUrl = Uri.parse(promoData.getPathUrl()).toString();
if (newUrl.contains("http")) {
startActivity(reDirect);
} else {
//if not do nothing
Log.d("Path URL: ", " is null");
}
}
});
recyclerView.setAdapter(new CustomListAdapter(this, listData));
recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//here is a method to either send the user to the poduct page
//or to the main page in the app
//open a new activity and close this one down
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem promoData = (ListItem) listView.getItemAtPosition(position);
//lest open up the corrisponding webpage
Intent reDirect = new Intent();
reDirect.setAction(Intent.ACTION_VIEW);
reDirect.addCategory(Intent.CATEGORY_BROWSABLE);
reDirect.setData(Uri.parse(promoData.getPathUrl()));
String newUrl = Uri.parse(promoData.getPathUrl()).toString();
if (newUrl.contains("http")) {
startActivity(reDirect);
} else {
//if not do nothing
Log.d("Path URL: ", " is null");
}
}
});
}
我将原始的 Listview 保留在那里,因为它可以很好地作为我正在做的事情的一个例子。
这是我的 xml 文件
<android.support.v7.widget.RecyclerView
android:id="@+id/promotionHolder"
android:layout_below="@+id/logout"
android:layout_width="match_parent"
android:layout_height="240dp"
android:orientation="horizontal"
android:layoutManager = "android.support.v7.widget.LinearLayoutManager"
>
</android.support.v7.widget.RecyclerView>
这是我的自定义列表适配器
public class CustomListAdapter extends BaseAdapter{
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.labelView = (TextView)convertView.findViewById(R.id.label);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem newsItem = listData.get(position);
//Check to see if there is a lable attached to the image
if(newsItem.getLableTitle().equals("null"))
{
holder.labelView.setText("");
}else
{
holder.labelView.setText(newsItem.getLableTitle());
}
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
}
return convertView;
}
static class ViewHolder {
ImageView imageView;
TextView labelView;
}
}
这是 list_row_layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="8dp"
android:padding="1dp"
android:background="#000000"
>
<ImageView
android:id="@+id/thumbImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#000000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/label"
android:layout_below="@id/thumbImage"
android:layout_marginTop="1dp"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:textSize="8pt"
/>
RecyclerView 不提供 #setOnItemClickListener
,RecyclerView.Adapter
也不提供。
您应该在 Adapters onBindViewHolder
方法中安装点击侦听器。因为我看不到上面那个方法的任何引用,我会认为你的 Adapter 还没有迁移到新的 RecyclerView 方法。
您可以参考互联网上的链接,例如 https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156 以了解如何执行此操作。
我正在尝试制作图片的水平滚动视图,并在每张图片上附加一个 OnClick 侦听器,将用户发送到新网页。我已经很好地处理了垂直列表,但现在我希望它是水平的。我怎样才能做到这一点。 我试过使用像 Android 建议的 recyclerView,但我无法让它接受适配器。
它给我这个错误: 回收器视图不能应用于 CustomeListAdpater,以及当我尝试添加 onClick 时它说它无法解析方法 setOnClickListern(anonymous android.widget.AdapterView.OnItemClickListener())
这是我目前所掌握的;
这里是线性布局管理器
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
recyclerView = (RecyclerView)findViewById(R.id.promotionHolder);
recyclerView.setLayoutManager(layoutManager);
这里是我将客户列表适配器分配给回收视图的地方
private void fillListView(ArrayList<ListItem> listData)
{
final ListView listView = (ListView) findViewById(R.id.custom_list);
listView.setAdapter(new CustomListAdapter(this, listData));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//here is a method to either send the user to the poduct page
//or to the main page in the app
//open a new activity and close this one down
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem promoData = (ListItem) listView.getItemAtPosition(position);
//lest open up the corrisponding webpage
Intent reDirect = new Intent();
reDirect.setAction(Intent.ACTION_VIEW);
reDirect.addCategory(Intent.CATEGORY_BROWSABLE);
reDirect.setData(Uri.parse(promoData.getPathUrl()));
String newUrl = Uri.parse(promoData.getPathUrl()).toString();
if (newUrl.contains("http")) {
startActivity(reDirect);
} else {
//if not do nothing
Log.d("Path URL: ", " is null");
}
}
});
recyclerView.setAdapter(new CustomListAdapter(this, listData));
recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//here is a method to either send the user to the poduct page
//or to the main page in the app
//open a new activity and close this one down
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem promoData = (ListItem) listView.getItemAtPosition(position);
//lest open up the corrisponding webpage
Intent reDirect = new Intent();
reDirect.setAction(Intent.ACTION_VIEW);
reDirect.addCategory(Intent.CATEGORY_BROWSABLE);
reDirect.setData(Uri.parse(promoData.getPathUrl()));
String newUrl = Uri.parse(promoData.getPathUrl()).toString();
if (newUrl.contains("http")) {
startActivity(reDirect);
} else {
//if not do nothing
Log.d("Path URL: ", " is null");
}
}
});
}
我将原始的 Listview 保留在那里,因为它可以很好地作为我正在做的事情的一个例子。
这是我的 xml 文件
<android.support.v7.widget.RecyclerView
android:id="@+id/promotionHolder"
android:layout_below="@+id/logout"
android:layout_width="match_parent"
android:layout_height="240dp"
android:orientation="horizontal"
android:layoutManager = "android.support.v7.widget.LinearLayoutManager"
>
</android.support.v7.widget.RecyclerView>
这是我的自定义列表适配器
public class CustomListAdapter extends BaseAdapter{
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.labelView = (TextView)convertView.findViewById(R.id.label);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem newsItem = listData.get(position);
//Check to see if there is a lable attached to the image
if(newsItem.getLableTitle().equals("null"))
{
holder.labelView.setText("");
}else
{
holder.labelView.setText(newsItem.getLableTitle());
}
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
}
return convertView;
}
static class ViewHolder {
ImageView imageView;
TextView labelView;
}
}
这是 list_row_layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="8dp"
android:padding="1dp"
android:background="#000000"
>
<ImageView
android:id="@+id/thumbImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#000000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/label"
android:layout_below="@id/thumbImage"
android:layout_marginTop="1dp"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:textSize="8pt"
/>
RecyclerView 不提供 #setOnItemClickListener
,RecyclerView.Adapter
也不提供。
您应该在 Adapters onBindViewHolder
方法中安装点击侦听器。因为我看不到上面那个方法的任何引用,我会认为你的 Adapter 还没有迁移到新的 RecyclerView 方法。
您可以参考互联网上的链接,例如 https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156 以了解如何执行此操作。