Android 使用自定义布局使用seekbar更改Listview文本大小

Android 使用自定义布局使用seekbar更改Listview文本大小,android,listview,seekbar,Android,Listview,Seekbar,我有一个底部带有seekbar的listview,seekbar的目的是调整listview的textsize,我的listview有一个自定义布局,textview在调整textsize时是我的目标。我解决了这个问题,但当我滑动seekbar时,只有第1、第4、第7行的文本被更改 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int Blast; @Ove

我有一个底部带有seekbar的listview,seekbar的目的是调整listview的textsize,我的listview有一个自定义布局,textview在调整textsize时是我的目标。我解决了这个问题,但当我滑动seekbar时,只有第1、第4、第7行的文本被更改

 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        int Blast;
        @Override

        public void onStopTrackingTouch(SeekBar seekBar){

            TextView shopName = (TextView)findViewById(R.id.item_version);
            prefs = getPreferences(MODE_PRIVATE);
            SharedPreferences.Editor ed = prefs.edit();
          ed.putFloat("fontsize", ((TextView) findViewById(R.id.item_version)).getTextSize());
            ed.commit();

           // myList.setSelection(1);
            ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX, seekBar.getProgress());

        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar){
            ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,seekBar.getProgress());
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser){

                ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,progress);

            Blast = progress;

        }
    });

public void viewAll1(){

        Cursor cursor = myDB.getAllData2();

        String[] from = new String[]{DBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.item_version};

        SimpleCursorAdapter myCursorAdapter =
                new SimpleCursorAdapter(this, R.layout.item_layout, cursor, from, to, 0);

    ListView myList = (ListView) findViewById(R.id.listView);

    myList.setAdapter(myCursorAdapter);

}
item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/item_version"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textStyle="bold"
    android:textIsSelectable="false"
    android:typeface="serif"

    android:paddingStart="20dp" />

</RelativeLayout>

问题在于,您的findViewById(R.id.item\u版本)只是查找布局中存在的多个R.id.item\u版本中的一个(每个元素一个)

我会改变方法,例如:

1) SeekBar将修改和存储/检索活动范围变量
2) 在适配器getView中,计算此变量以定义TextView文本的大小。

3) 更改Seekbar进度时:更改变量并调用适配器上的notifyDataSetChanged。

问题在于findViewById(R.id.item\u版本)只是查找布局中存在的多个R.id.item\u版本中的一个(每个元素一个)

我会改变方法,例如:

1) SeekBar将修改和存储/检索活动范围变量
2) 在适配器getView中,计算此变量以定义TextView文本的大小。

3) 更改Seekbar进度时:更改变量并在适配器上调用notifyDataSetChanged。

您需要在每个setTextSize之后添加此行

myAdapter.notifyDataSetChanged();
编辑:

public void viewAll1(){

        Cursor cursor = myDB.getAllData2();

        String[] from = new String[]{DBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.item_version};
        int layoutid=R.layout.item_version;
        myCursorAdapter= new cursorAdapter(context ,  layoutid,cursor,from, to);
        ListView myList = (ListView) findViewById(R.id.listView);

        myList.setAdapter(myCursorAdapter);

    }



public class cursorAdapter extends SimpleCursorAdapter {
        private Context appContext;
        private int layout;
        private Cursor mycursor;

        public cursorAdapter(Context context, int layout, Cursor c, String[] from,int[] to) {
            super(context, layout, c, from, to);
            this.appContext=context;
            this.layout=layout;
            this.mycursor=c;
        }

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

        @Override
        public Object getItem(int arg0) {
            return null;
        }
        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView textView = (TextView) view.findViewById(R.id.item_version);
            String title = cursor.getString( cursor.getColumnIndex( DBAdapter.KEY_NAME ) );
            textView.setText(title);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg);

        }



    }
因此,代码如下所示:

SimpleCursorAdapter myCursorAdapter; // define the adapter above onCreate method

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        int Blast;
        @Override

        public void onStopTrackingTouch(SeekBar seekBar){

            TextView shopName = (TextView)findViewById(R.id.item_version);
            prefs = getPreferences(MODE_PRIVATE);
            SharedPreferences.Editor ed = prefs.edit();
          ed.putFloat("fontsize", ((TextView) findViewById(R.id.item_version)).getTextSize());
            ed.commit();

           // myList.setSelection(1);
            ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX, seekBar.getProgress());
myCursorAdapter.notifyDataSetChanged();

        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar){
            ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,seekBar.getProgress());
myCursorAdapter.notifyDataSetChanged();
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser){

                ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,progress);
myCursorAdapter.notifyDataSetChanged();
            Blast = progress;

        }
    });

public void viewAll1(){

        Cursor cursor = myDB.getAllData2();

        String[] from = new String[]{DBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.item_version};

        myCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_layout, cursor, from, to, 0);

    ListView myList = (ListView) findViewById(R.id.listView);

    myList.setAdapter(myCursorAdapter);

}
EDIT2: 您需要在自定义适配器的getView中添加setTextSize,因为notifyDataSetChange实际上调用getView方法,这是一个工作示例,但当然使用了不同类型的适配器,即基本适配器:

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends Activity {
    SeekBar seekbar;
    cursorAdapter myCursorAdapter;
    String str[]={"Shop Name"};
    ListView list;
    TextView tv;
    int globProg=20; //used default value for textsize 20 you can put anything else
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(ListView) findViewById(R.id.listView);
        seekbar= (SeekBar) findViewById(R.id.seekBar);

        myCursorAdapter= new cursorAdapter(MainActivity.this,str); 
        list.setAdapter(myCursorAdapter);

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            int Blast;

            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {


                globProg= seekBar.getProgress(); //saving value in the global variable 
                myCursorAdapter.notifyDataSetChanged(); //this method calls the getview again so the view is recreated with the new size

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                globProg= seekBar.getProgress();
                myCursorAdapter.notifyDataSetChanged();
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                globProg= seekBar.getProgress();
                myCursorAdapter.notifyDataSetChanged();
                Blast = progress;

            }
        });

    }// end of onCreate 

    public class cursorAdapter extends BaseAdapter
    {
        public String list[];
        Activity activity;

        public cursorAdapter(Activity activity,String list[]) {
            super();
            this.activity = activity;
            this.list = list;

        }
        @Override
        public int getCount() {
            return list.length;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        private class ViewHolder {
            TextView textView;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            LayoutInflater inflater =  activity.getLayoutInflater();

            convertView = inflater.inflate(R.layout.item_version, null);
            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.item_version);
            holder.textView.setText(list[position]);
            holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg); // setTextSize here 
            convertView.setTag(holder);

            return convertView;
        }


    }






}
EDIT3:

public void viewAll1(){

        Cursor cursor = myDB.getAllData2();

        String[] from = new String[]{DBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.item_version};
        int layoutid=R.layout.item_version;
        myCursorAdapter= new cursorAdapter(context ,  layoutid,cursor,from, to);
        ListView myList = (ListView) findViewById(R.id.listView);

        myList.setAdapter(myCursorAdapter);

    }



public class cursorAdapter extends SimpleCursorAdapter {
        private Context appContext;
        private int layout;
        private Cursor mycursor;

        public cursorAdapter(Context context, int layout, Cursor c, String[] from,int[] to) {
            super(context, layout, c, from, to);
            this.appContext=context;
            this.layout=layout;
            this.mycursor=c;
        }

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

        @Override
        public Object getItem(int arg0) {
            return null;
        }
        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView textView = (TextView) view.findViewById(R.id.item_version);
            String title = cursor.getString( cursor.getColumnIndex( DBAdapter.KEY_NAME ) );
            textView.setText(title);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg);

        }



    }

您需要在每个setTextSize之后添加此行

myAdapter.notifyDataSetChanged();
编辑:

public void viewAll1(){

        Cursor cursor = myDB.getAllData2();

        String[] from = new String[]{DBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.item_version};
        int layoutid=R.layout.item_version;
        myCursorAdapter= new cursorAdapter(context ,  layoutid,cursor,from, to);
        ListView myList = (ListView) findViewById(R.id.listView);

        myList.setAdapter(myCursorAdapter);

    }



public class cursorAdapter extends SimpleCursorAdapter {
        private Context appContext;
        private int layout;
        private Cursor mycursor;

        public cursorAdapter(Context context, int layout, Cursor c, String[] from,int[] to) {
            super(context, layout, c, from, to);
            this.appContext=context;
            this.layout=layout;
            this.mycursor=c;
        }

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

        @Override
        public Object getItem(int arg0) {
            return null;
        }
        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView textView = (TextView) view.findViewById(R.id.item_version);
            String title = cursor.getString( cursor.getColumnIndex( DBAdapter.KEY_NAME ) );
            textView.setText(title);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg);

        }



    }
因此,代码如下所示:

SimpleCursorAdapter myCursorAdapter; // define the adapter above onCreate method

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        int Blast;
        @Override

        public void onStopTrackingTouch(SeekBar seekBar){

            TextView shopName = (TextView)findViewById(R.id.item_version);
            prefs = getPreferences(MODE_PRIVATE);
            SharedPreferences.Editor ed = prefs.edit();
          ed.putFloat("fontsize", ((TextView) findViewById(R.id.item_version)).getTextSize());
            ed.commit();

           // myList.setSelection(1);
            ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX, seekBar.getProgress());
myCursorAdapter.notifyDataSetChanged();

        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar){
            ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,seekBar.getProgress());
myCursorAdapter.notifyDataSetChanged();
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser){

                ((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,progress);
myCursorAdapter.notifyDataSetChanged();
            Blast = progress;

        }
    });

public void viewAll1(){

        Cursor cursor = myDB.getAllData2();

        String[] from = new String[]{DBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.item_version};

        myCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_layout, cursor, from, to, 0);

    ListView myList = (ListView) findViewById(R.id.listView);

    myList.setAdapter(myCursorAdapter);

}
EDIT2: 您需要在自定义适配器的getView中添加setTextSize,因为notifyDataSetChange实际上调用getView方法,这是一个工作示例,但当然使用了不同类型的适配器,即基本适配器:

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends Activity {
    SeekBar seekbar;
    cursorAdapter myCursorAdapter;
    String str[]={"Shop Name"};
    ListView list;
    TextView tv;
    int globProg=20; //used default value for textsize 20 you can put anything else
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(ListView) findViewById(R.id.listView);
        seekbar= (SeekBar) findViewById(R.id.seekBar);

        myCursorAdapter= new cursorAdapter(MainActivity.this,str); 
        list.setAdapter(myCursorAdapter);

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            int Blast;

            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {


                globProg= seekBar.getProgress(); //saving value in the global variable 
                myCursorAdapter.notifyDataSetChanged(); //this method calls the getview again so the view is recreated with the new size

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                globProg= seekBar.getProgress();
                myCursorAdapter.notifyDataSetChanged();
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                globProg= seekBar.getProgress();
                myCursorAdapter.notifyDataSetChanged();
                Blast = progress;

            }
        });

    }// end of onCreate 

    public class cursorAdapter extends BaseAdapter
    {
        public String list[];
        Activity activity;

        public cursorAdapter(Activity activity,String list[]) {
            super();
            this.activity = activity;
            this.list = list;

        }
        @Override
        public int getCount() {
            return list.length;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        private class ViewHolder {
            TextView textView;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            LayoutInflater inflater =  activity.getLayoutInflater();

            convertView = inflater.inflate(R.layout.item_version, null);
            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.item_version);
            holder.textView.setText(list[position]);
            holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg); // setTextSize here 
            convertView.setTag(holder);

            return convertView;
        }


    }






}
EDIT3:

public void viewAll1(){

        Cursor cursor = myDB.getAllData2();

        String[] from = new String[]{DBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.item_version};
        int layoutid=R.layout.item_version;
        myCursorAdapter= new cursorAdapter(context ,  layoutid,cursor,from, to);
        ListView myList = (ListView) findViewById(R.id.listView);

        myList.setAdapter(myCursorAdapter);

    }



public class cursorAdapter extends SimpleCursorAdapter {
        private Context appContext;
        private int layout;
        private Cursor mycursor;

        public cursorAdapter(Context context, int layout, Cursor c, String[] from,int[] to) {
            super(context, layout, c, from, to);
            this.appContext=context;
            this.layout=layout;
            this.mycursor=c;
        }

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

        @Override
        public Object getItem(int arg0) {
            return null;
        }
        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView textView = (TextView) view.findViewById(R.id.item_version);
            String title = cursor.getString( cursor.getColumnIndex( DBAdapter.KEY_NAME ) );
            textView.setText(title);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg);

        }



    }

您需要重新加载listview才能正确查看影响。感谢您的回复,。请指导我如何操作:)您需要重新加载listview才能正确查看影响。感谢您的回复,。请指导我如何做:)非常感谢您的回复我是android开发新手:)您能看看我在适配器中的代码吗?我已经更新了上面的代码供你们查看。非常感谢你们的回复。我是android开发新手:)你们能在适配器中查看我的代码吗?我已经更新了上面的代码,希望你乐于帮助,这对我很有效,如果它对你的案例有效,请接受它作为答案:D谢谢你做了这样一个项目?它仍然不起作用:(不是完全相同的项目,你的适配器是自定义适配器吗?你的listview包含1项或更多项?如果更多项,你想更改所有项的文本大小吗?请查看编辑2这是我尝试的代码:)是的,我有多个项,我想更改它们的文本大小,我使用了自定义adapterGlad来帮助,这对我来说很有效,如果对你的情况有效,请接受它作为答案:D谢谢你做了这样一个项目?它仍然不起作用:(不是完全相同的项目,你的适配器是自定义适配器吗?你的listview包含1项或更多项?如果更多项,你想更改所有项的文本大小吗?请检查编辑2这是我尝试的代码:)是的,我有多个项,我想更改它们的文本大小,我使用了自定义适配器