android布局列表视图建议
我目前有一个程序,当用户单击按钮时显示listview。这个视图占据了整个屏幕。如何将主屏幕分成两部分,一部分是大的listview,两部分是几个按钮,而不必按按钮进入listview 以下是我使用listView适配器的方式: 公共空间显示弹出窗口{ ListArrayAdapter=新建ListArrayAdapter此,R.layout.list\u view\u row\u项,foodListandroid布局列表视图建议,android,listview,layout,Android,Listview,Layout,我目前有一个程序,当用户单击按钮时显示listview。这个视图占据了整个屏幕。如何将主屏幕分成两部分,一部分是大的listview,两部分是几个按钮,而不必按按钮进入listview 以下是我使用listView适配器的方式: 公共空间显示弹出窗口{ ListArrayAdapter=新建ListArrayAdapter此,R.layout.list\u view\u row\u项,foodList ListView listViewItems = new ListView(this); li
ListView listViewItems = new ListView(this);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(new OnListItemClick());
alertDialogStores = new AlertDialog.Builder(MainScreen.this)
.setView(listViewItems)
.setTitle("Food List")
.show();
}
这是适配器:
public class ListArrayAdapter extends ArrayAdapter<ListItem> {
Context mContext;
int layoutResourceId;
ListItem data[] = null;
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
ArrayList<ListItem> foodList = new ArrayList<>();
//public ArrayAdapterItem(Context mContext, int layoutResourceId, ListItem[] data) {
public ListArrayAdapter(Context mContext, int layoutResourceId, ArrayList<ListItem> foodList) {
super(mContext, layoutResourceId, foodList);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.foodList = foodList;
//this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* The convertView argument is essentially a "ScrapView" as described is Lucas post
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
* It will have a non-null value when ListView is asking you recycle the row layout.
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
*/
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
// object item based on the position
//ListItem ListItem = data[position];
// get the TextView and then set the text (item name) and tag (item ID) values
TextView textViewItem = (TextView) convertView.findViewById(R.id.itemName);
textViewItem.setText(foodList.get(position).getItemName());
textViewItem.setTag(foodList.get(position).getItemIdInt());
progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar1);
return convertView;
}
}
我还想知道,当用户按住一个项目时,是否可能出现一条消息,并在他们放开后让它停留一段时间。您可以使用android:layout\u weight属性
如果答案对你有帮助,就接受它
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</LinearLayout>
如何在此设置中使用listView适配器?我可以在xml中分配它吗?你可以发布你的完整代码吗?使用toast显示消息。选中编辑的答案,单击listview中的项目即可显示消息。谢谢!这正是我需要的。没问题,很高兴它帮助了你。即使你接受了答案,你也可以投上一票。
listViewItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String str = adapterView.getItemAtPosition(arg2).toString();
Toast.makeText(getApplicationContext(), "You clicked "+ str, 100).show();
}
}
String str = foodList.get(arg2).getItemName().toString() + " " +foodList.get(arg2).getItemIdInt();