gpt4 book ai didi

java - 如何将 ListView 转换为任何类型的简单水平滚动条?

转载 作者:行者123 更新时间:2023-12-02 02:14:28 26 4
gpt4 key购买 nike

我正在向我的应用程序添加一个包含大约 10 个图标的启动器,并让它垂直工作,但找不到可靠的简单解决方案来旋转它,以便它水平滚动 10 个图标。

Java:

private PackageManager manager;
private List<AppDetail> apps;
private ListView list;

private void loadListView(){
//list = (ListView)findViewById(R.id.apps_list);//was apps_list
list = (ListView) findViewById(R.id.apps_list);//was apps_list

ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this,
R.layout.list_item,
apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}

ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
appIcon.setImageDrawable(apps.get(position).icon);

TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
appLabel.setText(apps.get(position).label);

TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
appName.setText(apps.get(position).name);

return convertView;
}
};
list.setAdapter(adapter); //Put the list on the screen
}

XML 布局内部

<ListView
android:id="@+id/apps_list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="50dp"
android:layout_marginEnd="29dp"
android:orientation="horizontal"/>

我看到了一个叫做 Horizo​​ntalScrollView 的东西,但它似乎不是直接替代品,只是导致应用程序崩溃。

看起来我应该使用 RecyclerView 或者类似的东西。

Java:

private void loadListView(){


mRecyclerView = (RecyclerView) findViewById(R.id.apps_list);
ArrayAdapter<AppDetail> mAdapter = new ArrayAdapter<AppDetail>(this,
R.layout.list_item,
apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}

ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
appIcon.setImageDrawable(apps.get(position).icon);

TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
appLabel.setText(apps.get(position).label);

TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
appName.setText(apps.get(position).name);

return convertView;
}
};
mRecyclerView.setAdapter(mAdapter); //Put the list on the screen
}

XML:

<android.support.v7.widget.RecyclerView
android:id="@+id/apps_list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="50dp"
android:layout_marginEnd="29dp"
android:orientation="horizontal"
android:scrollbars="horizontal"/>

但我收到错误:

Error:(1212, 34) error: incompatible types: ArrayAdapter cannot be converted to Adapter

感谢您的指导。

最佳答案

我已将您的 ListView 实现为回收器 View ,这是适配器:

public class SpecialAdapter extends RecyclerView.Adapter<SpecialAdapter.SpecialViewHolder> {

private ArrayList<AppDetail> items;
private Context context;
private OnClickItem clickListener;

public SpecialAdapter(ArrayList<AppDetail> items, Context context) {
this.items = items;
this.context = context;
}

public void setClickListener(OnClickItem clickListener) {
this.clickListener = clickListener;
}

@NonNull
@Override
public SpecialViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new SpecialViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull SpecialViewHolder holder, int position) {
holder.appIcon.setImageDrawable(items.get(position).icon);
holder.appLabel.setText(items.get(position).label);
holder.appName.setText(items.get(position).name);

}

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

class SpecialViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView appIcon;
TextView appLabel;
TextView appName;

public SpecialViewHolder(View itemView) {
super(itemView);
appIcon = (ImageView) itemView.findViewById(R.id.item_app_icon);
appLabel = (TextView) itemView.findViewById(R.id.item_app_label);
appName = (TextView) itemView.findViewById(R.id.item_app_name);
itemview.setOnClickListener(this);
}

@Override
public void onClick(View v) {
clickListener.onClickItem(getAdapterPosition());
}
}

public interface OnClickItem {
void onClickItem(int pos);
}
}

要使用它,只需将 xml 中的 ListView 替换为 RecyclerVie 并使用:

public void setUpRecyclerView() {
RecyclerView list = findViewById(R.id.apps_list);

list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
SpecialAdapter adapter = new SpecialAdapter(apps,this);
adapter.setClickListener(this);
list.setAdapter(adapter);

}

此外,让您的 Activity 实现 OnClickItem,以便能够捕获列表中项目的点击。

关于java - 如何将 ListView 转换为任何类型的简单水平滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49502359/

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