簡體   English   中英

Android ListView無法滾動,自定義適配器不好?

[英]Android ListView can't scroll more, bad custom Adapter?

EDIT2: 現在我已經為布局設計了一個合適的圖形並且可以滾動 ,但是我的自定義適配器存在新問題。 當我滾動時,我的editText失去了它們的內容,並被其他元素中的其他內容所替代。 但是,當我使用SimpleCursorAdapter時,則沒有任何問題(但是我無法使用按鈕)。

這是我的自定義適配器:

private class MySimpleCursorAdapter extends SimpleCursorAdapter
    {
        ViewHolder vh;
        public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
            super(context, layout, c, from, to, flags);
            cursor = c;
        }

        public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.card_stock, parent, false);
            vh = new ViewHolder();
            vh.idList = (TextView) view.findViewById(R.id.idList);
            vh.themeList = (TextView) view.findViewById(R.id.themeList);
            vh.questionList = (TextView) view.findViewById(R.id.questionList);
            vh.reponseList = (TextView) view.findViewById(R.id.reponseList);
            vh.difficulteList = (TextView) view.findViewById(R.id.difficultList);
            vh.Supprimer = (Button) view.findViewById(R.id.buttonDelete);
            vh.Modifier = (Button) view.findViewById(R.id.buttonModifier);


            view.setTag(vh);
            return view;
        }

        public void bindView(View view, Context Context, Cursor cursor) {
                vh.idList.setText(cursor.getString(cursor.getColumnIndex(stock._ID)));
                vh.themeList.setText(cursor.getString(cursor.getColumnIndex(stock.THEME)));
                vh.questionList.setText(cursor.getString(cursor.getColumnIndex(stock.QUESTION)));
                vh.reponseList.setText(cursor.getString(cursor.getColumnIndex(stock.REPONSE)));
                vh.difficulteList.setText(cursor.getString(cursor.getColumnIndex(stock.DIFFICULTE)));



            vh.Supprimer.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);

                    int id = Integer.parseInt(idList.getText().toString());
                    deleteOneCard(id);
                    Toast.makeText(container.getContext(), "Suppression de "+id, Toast.LENGTH_SHORT).show();
                }
            });

            vh.Modifier.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);
                    TextView themeList = (TextView) parentView.findViewById(R.id.themeList);
                    themeList.setFocusable(true);
                    themeList.requestFocus();
                    /*TextView questionList = (TextView) parentView.findViewById(R.id.questionList);
                    TextView reponseList = (TextView) parentView.findViewById(R.id.reponseList);
                    TextView difficulteList = (TextView) parentView.findViewById(R.id.difficultList);*/

                    int id = Integer.parseInt(idList.getText().toString());
                    Toast.makeText(container.getContext(), "bouton Modifier pour "+id, Toast.LENGTH_SHORT).show();
                }
            });

        }
    }



    public class ViewHolder
    {
        Button Supprimer, Modifier;
        TextView idList, themeList, questionList, reponseList, difficulteList;

    }

感謝大伙們。

ListView高度設置為match_parent 因此,它將占據父View的整個區域。

 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:scrollingCache="true"/>

嘗試將所有可滾動的內容添加到單個RelativeLayout中,並將此內容添加到ScrollView中。

<ScrollView>
    <RelativeLayout>
        // other elements

要么

<ScrollView>
    <ListView>

第一個示例中的RelativeLayout是必需的,因為ScrollView元素僅接受一個孩子。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@android:color/darker_gray">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollingCache="true"/>

</RelativeLayout>

要么

使用LinearLayout並將ListView高度更改為0dp並添加weight=1

<LinearLayout 
........
              android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollingCache="true"/>

</LinearLayout>

我有解決我的問題! 在擴展SimpleCursorAdapter的內部類中,在我的bindView函數中,我不得不再次初始化我的editText! 像這樣 :

public void bindView(View view, Context Context, Cursor cursor) {
            //EDIT
            vh.idList = (TextView) view.findViewById(R.id.idList);
            vh.themeList = (TextView) view.findViewById(R.id.themeList);
            vh.questionList = (TextView) view.findViewById(R.id.questionList);
            vh.reponseList = (TextView) view.findViewById(R.id.reponseList);
            vh.difficulteList = (TextView) view.findViewById(R.id.difficultList);
            vh.Supprimer = (Button) view.findViewById(R.id.buttonDelete);
            vh.Modifier = (Button) view.findViewById(R.id.buttonModifier);
            //END EDIT

            vh.idList.setText(cursor.getString(cursor.getColumnIndex(stock._ID)));
            vh.themeList.setText(cursor.getString(cursor.getColumnIndex(stock.THEME)));
            vh.questionList.setText(cursor.getString(cursor.getColumnIndex(stock.QUESTION)));
            vh.reponseList.setText(cursor.getString(cursor.getColumnIndex(stock.REPONSE)));
            vh.difficulteList.setText(cursor.getString(cursor.getColumnIndex(stock.DIFFICULTE)));



            vh.Supprimer.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);

                    int id = Integer.parseInt(idList.getText().toString());
                    deleteOneCard(id);
                    Toast.makeText(container.getContext(), "Suppression de "+id, Toast.LENGTH_SHORT).show();
                }
            });

            vh.Modifier.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);
                    TextView themeList = (TextView) parentView.findViewById(R.id.themeList);
                    themeList.setFocusable(true);
                    themeList.requestFocus();
                    /*TextView questionList = (TextView) parentView.findViewById(R.id.questionList);
                    TextView reponseList = (TextView) parentView.findViewById(R.id.reponseList);
                    TextView difficulteList = (TextView) parentView.findViewById(R.id.difficultList);*/

                    int id = Integer.parseInt(idList.getText().toString());
                    Toast.makeText(container.getContext(), "bouton Modifier pour "+id, Toast.LENGTH_SHORT).show();
                }
            });

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM