【问题标题】:Change Listview textsize with seekbar using custom layout使用自定义布局使用搜索栏更改 Listview 文本大小
【发布时间】:2015-11-05 12:55:32
【问题描述】:

我有一个底部有seekbar的listview,seekbar的目的是调整listview的textsize,我的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>

【问题讨论】:

  • 您需要重新加载列表视图才能正确查看效果。
  • 感谢您的回复,。请指导我如何做到这一点:)

标签: android listview seekbar


【解决方案1】:

你需要在每个 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);

        }



    }

【讨论】:

  • 很高兴为您提供帮助,它对我有用,如果它适用于您的情况,请接受它作为答案:D 谢谢
  • 你做了一个这样的项目?它仍然不起作用:(
  • 不完全相同的项目,您的适配器是自定义适配器吗?您的列表视图包含 1 项或更多项?如果您想更改所有这些的文字大小?
  • 请查看编辑 2 这是我尝试过的代码 :)
  • 是的,我有多个项目,我想更改它们的文本大小,我使用了自定义适配器
【解决方案2】:

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

我会改变方法,例如:

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

【讨论】:

  • 非常感谢您的回复,我是 android 开发新手 :) 您能看看我在适配器中的代码吗?我已经更新了上面的代码供您查看
猜你喜欢
  • 2017-10-18
  • 1970-01-01
  • 2013-10-25
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 2016-01-31
  • 1970-01-01
相关资源
最近更新 更多