ListView中因layout_height是wrap_content而引发的问题 10C

问题描述:
先是点击画面上的一个按钮,然后出现下方弹窗
图一:点击弹窗内的“show preview”,会出现listview,如图二
图一:点击弹窗内的“show preview”,会出现listview,如图二
图二:listview出现之后,快速点击任意item,按照设计应该是出现图一的画面
图二:listview出现之后,快速点击任意item,按照设计应该是出现图一的画面
但是实际上出现的是图三的画面,
图三:图中出现了和listview大小一样的透明层
图三:图中出现了和listview大小一样的透明层
这种现象只会在listview初始化的时候并且是快速点击下才会出现,往后怎么点击都不会出现。
现在怀疑这个问题可能是因为ListView中的layout_height是wrap_content而引发的问题,改成具体fill_parem或者是具体的高度就能够解决,但是要怎么证明这个现象是由这个问题导致的呢?
代码如下:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/flipper_setting"
    android:layout_width="fill_parent"
    android:layout_height="400px"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:background="@color/gold"

    >

    <!-- setting category page -->
    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="400px"
        android:gravity="top"
        android:orientation="vertical">

        <!-- Preview Setting -->
        <include
            android:id="@+id/include_setting_preview"
            layout="@layout/list_row_setting_category" />

    </LinearLayout>

    <!-- detail setting page -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="400px"
        android:gravity="top"
        android:orientation="vertical"
        android:id="@+id/layout2">
        <TextView
            android:id="@+id/text_title_detail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textSize = "20px"/>
        <com.example.viewflippertest.ListViewConfig
            android:id="@+id/listview_detail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/white" />

    </LinearLayout>

 </LinearLayout>

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.id_btn) ;
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Dialog dialog = createOtherSettingDialog(MainActivity.this);
                dialog.show();
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = 600;
                lp.height = 500;
                dialog.getWindow().setAttributes(lp);

            }
        });

    }


    public static AlertDialog createOtherSettingDialog(final Context context) {

        //(1)
        LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(R.layout.dlg_setting_other, null);

        final ListViewConfig listView_detail = (ListViewConfig)view.findViewById(R.id.listview_detail);
        final TextView text_detail_title = (TextView)view.findViewById(R.id.text_title_detail);
        final LinearLayout layout2 = (LinearLayout) view.findViewById(R.id.layout2);
        final LinearLayout layout1 = (LinearLayout) view.findViewById(R.id.layout1);
        layout2.setVisibility(View.GONE);

        //(2)
        /*============ Preview Setting ================*/

        final LinearLayout layout_preview = (LinearLayout)view.findViewById(R.id.include_setting_preview);
        final ArrayAdapter<String> adapter_preview = new adaper(context, R.layout.list_row_setting_detail, R.id.text_detail);
        final TextView text_category_preview = (TextView)layout_preview.findViewById(R.id.text_category);
        final TextView text_value_preview = (TextView)layout_preview.findViewById(R.id.text_value);
        text_category_preview.setText(context.getString(R.string.txid_scan_b_other_preview_title));
        text_value_preview.setText("On");

        final List<String> list_preview = new ArrayList<String>();
        list_preview.add(0, "On");
        list_preview.add(1, "Off");
        for(int i=0; i<list_preview.size(); ++i) {
            adapter_preview.add(list_preview.get(i));
        }
        layout_preview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("========= layout_preview onclick");

                text_detail_title.setText(R.string.txid_scan_b_other_preview_title);
                listView_detail.setAdapter(adapter_preview);
                layout1.setVisibility(View.GONE);
                layout2.setVisibility(View.VISIBLE);


            }
        });

        /*===============================================*/

        //(3)
        listView_detail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("========= listView_detail onclick");
                if(parent.getAdapter().equals(adapter_preview)) {
                    // Update preview setting
                    String value = adapter_preview.getItem(position);
                    TextView text_value = (TextView)layout_preview.findViewById(R.id.text_value);
                    text_value.setText(value);

                } else {
                    /* should never reach this point */
                }
                layout1.setVisibility(View.VISIBLE);
                layout2.setVisibility(View.GONE);
            }
        });

        //(4)
        final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(context.getString(R.string.txid_scan_t_top_other_title));
        dialog.setNegativeButton(context.getString(R.string.txid_cmn_b_close), null);
        dialog.setView(view);
        return dialog.create();
    }




    private static class adaper extends ArrayAdapter<String>{


        public adaper(Context context, int resource, int textViewResourceId) {
            super(context, resource, textViewResourceId);
            // TODO Auto-generated constructor stub
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("========= position:" + position + ",convertView:" + convertView +",parent:" + parent.getMeasuredHeight() +",parent.getChildCount:" + parent.getChildCount());
            return super.getView(position, convertView, parent);
        }

    }

3个回答

wrap_content在绘制时会计算item高度,fill_parem自适应充满,由所包含的子view的高度撑起来,这样绘制比较快。list的数据量较大的情况下,用肉眼可以明显看出来fill_parem加载比较迅速。

sinat_29957455
修炼之路 数据量不大,只有3条数据。或者会是由其他问题导致的?
大约 2 年之前 回复

点击后要通知更新的,adapter.notifyDataSetChanged

sinat_29957455
修炼之路 应该是不需要进行更新的,因为我只是将View进行GONE或VISIBLE
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问