android布局listview建议

时间:2014-05-09 04:18:29

标签: android listview layout

我目前有一个程序,当用户点击按钮时会显示一个列表视图。此视图占据整个屏幕。我怎样才能(不必按一个按钮进入列表视图)将主屏幕分为两部分,一部分是大型列表视图,两部分是几个按钮?

这就是我使用listView适配器的方式:     public void showPopUp(){     ListArrayAdapter adapter = new ListArrayAdapter(this,R.layout.list_view_row_item,foodList);

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;

}

}
我还想知道当用户按下并按住某个项目时是否可以显示一条消息(并且在他们放手后让它稍微停留一段时间)。

1 个答案:

答案 0 :(得分:0)

您可以使用android:layout_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 **中点击项目获取消息

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


 }
    }

您可以使用

代替adapterview
String str = foodList.get(arg2).getItemName().toString() + " " +foodList.get(arg2).getItemIdInt();