使用自定义布局更改Listview使用搜索栏进行文本化

时间:2015-11-05 12:55:33

标签: android listview seekbar

我有一个listview,底部有一个搜索栏,搜索栏的目的是调整listview的文本大小,我的listview有一个自定义布局,textview在调整textsize时有我的目标。我解决了这个问题,但当我滑动搜索栏时,只更改了第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>

2 个答案:

答案 0 :(得分:0)

问题是你的findViewById(R.id.item_version)只是找到你的布局中存在的多个R.id.item_version之一(每个元素一个)。

我会改变方法,例如:

1)SeekBar将修改和存储/检索活动范围变量
2)在适配器getView中,您可以评估此​​变量以定义TextView文本的大小 3)当Seekbar进度发生变化时:更改变量并在适配器上调用notifyDataSetChanged。

答案 1 :(得分:0)

您需要在每个setTextSize

之后添加此行
myAdapter.notifyDataSetChanged();

修改

所以代码看起来像这样:

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);

        }



    }