10

以下のコードは、すべての行を取得するのではなく、表示されている行のみを取得するため、すべての行のテキストを変更しませListView'sん。 getChildCount()ListView's

for (int i = 0; i < listView.getChildCount(); i++)
{
    View v = listView.getChildAt(i);
    TextView tx = (TextView) v.findViewById(R.id.mytext);
    tx.setTextSize(newTextSize);
}

だから、私は何をすべきですか?

行が表示されたときに通知を受け取るためのコードがあるListView'sので、そのテキストサイズを設定できますか?

4

4 に答える 4

15

ListViewでは、表示されているのは子だけです。「すべての子」で何かをしたい場合は、アダプターでそれを行います。それは最高の場所です。

于 2010-01-22T06:21:42.863 に答える
9

APIデモのList13は、OnScrollStateChangedを使用して同様のことを行います。ただし、もっと良い方法があるかもしれません。

public void onScrollStateChanged(AbsListView view, int scrollState) {
    switch (scrollState) {
    case OnScrollListener.SCROLL_STATE_IDLE:
        mBusy = false;

        int first = view.getFirstVisiblePosition();
        int count = view.getChildCount();
        for (int i=0; i<count; i++) {
            TextView t = (TextView)view.getChildAt(i);
            if (t.getTag() != null) {
                t.setText(mStrings[first + i]);
                t.setTag(null);
            }
        }

        mStatus.setText("Idle");
        break;

。。。

Corey Tragerによる編集:

上記は間違いなく私を正しい方向に向けました。OnScrollListener.onScrollの処理はonScrollStateChangedよりもうまく機能することがわかりました。onScrollSgtaetChangedのcaseステートメントを削除し、すべての状態変更を処理しても、一部のテキストのサイズが変更されませんでした。しかし、onScrollを使用すると、動作するように見えます。

したがって、一見機能しているように見えるコードは次のようになります。

public void onScroll(AbsListView v, int firstVisibleItem, int visibleCount, int totalItemCount)
{
    ListView lv = this.getListView();
    int childCount = lv.getChildCount();

    for (int i = 0; i < childCount; i++)
    {
        View v = lv.getChildAt(i);
        TextView tx = (TextView) v.findViewById(R.id.mytext);
        tx.setTextSize(textSize);
    }
}
于 2010-01-22T01:54:36.897 に答える
0

パフォーマンス効果についてはよくわかりませんが、これを試すことができます。

//declare the list
ListView list = (ListView)findViewById(R.id.yourListID);

// in my case i have spinners and made a test just to know if this works
 Spinner spnWhatever;
 View view;
 int i =0;


       do{

           view =list.getAdapter().getView(i,null,null);
           //get the spinners from each row
           spnWhatever =(Spinner)view.findViewById(R.id.spnTarifas);

        i++;

       }while(list.getAdapter().getCount() !=i);
于 2016-04-25T11:16:36.573 に答える
0
//Create layout file containing views you want to read input from
//layout/gen_edt_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parLay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/edtGName"
        android:layout_width = "match_parent"
        android:layout_height ="wrap_content"
        android:hint="Name"
        android:inputType="text" />


    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/edtGIdPass"
        style="@style/edt"
        android:hint="Id number"
        android:inputType="number" />


</LinearLayout>

//Create Empty Activity -> MultipleInput

//layout/activity_multiple_input.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OpenAccount">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">


      <ListView
            android:id="@+id/inputLView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="60dp" />

    </LinearLayout>

    <Button
        android:layout_width="wrap_content
        android:layout_height="wrap_content"
        android:text="Read"
        android:onClick="readInput"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent>

</androidx.constraintlayout.widget.ConstraintLayout>

public class MultipleInput extends AppCompatActivity {

    //Declare and intialize arrays to store input fields as public modifiers
    ArrayList<TextInputEditText> edtNameArr = new ArrayList<>();
    ArrayList<TextInputEditText> edtIdsArr = new ArrayList<>();

    //Intialize arrays to store input values as public modifiers
    ArrayList<String> arr0 = new ArrayList<>();
    ArrayList<String> arr1 = new ArrayList<>();

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        listView = findViewById(R.id.listView);

        edtNameArr.clear();
        edtIdsArr.clear();

        arr0.clear();
        arr1.clear();

        //set number of group fields to be generated
        int members = 4;

        //initialize class passing fields number and context
        edtAdapter = new EdtAdapter(members, getApplicationContext());

        //Set retured fields in listView
        listView.setAdapter(edtAdapter);


    }

    public class EdtAdapter extends BaseAdapter {

        class ViewHolder {

            TextInputEditText edtGName, edtGIdPass;
        }

        public Context context;

        ArrayList<EdtClass> edtArrayList;
        int members;

        EdtAdapter(int members, Context context) {

            this.members = members;
            this.context = context;

        }

        @Override
        public int getCount() {
            return members;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View contentView, ViewGroup parent) {

            View rowView = contentView;

            if (rowView == null) {

                viewHolder = new ViewHolder();

                LayoutInflater inflater = getLayoutInflater();
                rowView = inflater.inflate(R.layout.gen_edt_layout, parent, false);
                viewHolder.edtGName = rowView.findViewById(R.id.edtGName);
                viewHolder.edtGIdPass = rowView.findViewById(R.id.edtGIdPass);

                viewPosition = position;

                //loop adding input fields in arrays
                for (int i = 0; i < members; i++) {
                    edtNameArr.add(viewHolder.edtGName);
                    edtIdsArr.add(viewHolder.edtGIdPass);
                }


            } else {
                viewHolder = (ViewHolder) contentView.getTag();
            }

            return rowView;

        }

    }


    //Read input when button clicked
    public void readInput(View view){


        for(int edt = 0; edt<edtNameArr.size(); edt++){

            String name = Objects.requireNonNull(edtNameArr.get(edt).getText()).toString();

            if(!arr0.contains(name)) {
                arr0.add(name);
            }

            String ids = Objects.requireNonNull(edtIdsArr.get(edt).getText()).toString();

            if(!arr1.contains(ids)) {
                arr1.add(ids);
            }

        }

        StringBuilder name = new StringBuilder();
        StringBuilder idno = new StringBuilder();

        //loop through arrays with input, reading and appending values in StringBuilder
        for (int m = 0; m < arr0.size(); m++) {

            name.append(arr0.get(m)).append(",");

            idno.append(arr1.get(m)).append(",");

        }

        //Show input values in logcat/Toast
        Log.e("Names ", name.toString());
        Log.e("Ids ", idno.toString());
        Toast.makeText(MultipleInput.this, "Names "+ name.toString(), Toast.LENGTH_LONG).show();

    }
于 2020-02-17T07:45:18.067 に答える