【问题标题】:android layout listview adviceandroid布局listview建议
【发布时间】:2014-06-26 17:12:16
【问题描述】:

我目前有一个程序可以在用户单击按钮时显示列表视图。这个视图占据了整个屏幕。我怎样才能(而不是必须按下按钮进入列表视图)将主屏幕分成两部分,一个是大列表视图,两个是几个按钮?

这就是我使用 listView 适配器的方式: 公共无效showPopUp(){ ListArrayAdapter 适配器 = 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;

}

}
我也想知道当用户按住一个项目时是否有可能出现一条消息(并在他们放手后让它徘徊一点)。

【问题讨论】:

    标签: android listview layout


    【解决方案1】:

    您可以使用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();
    

    【讨论】:

    • 如何在此设置中使用我的 listView 适配器?我可以在 xml 中分配它吗?
    • 你能发布你的完整代码吗?使用 toast 来显示消息。
    • 检查编辑的答案以在单击列表视图中的项目时显示消息。
    • 谢谢!这正是我所需要的
    • 没问题,很高兴它帮助了你:)。即使接受了答案,您也可以对答案进行投票。
    猜你喜欢
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多