更改搜索栏上的ListView项值更改

时间:2016-12-15 07:20:58

标签: android listview seekbar

如何在Seekbar进度更改上更改ListView项目值。

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


    <SeekBar
        android:id="@+id/seekValue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:max="100" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp">


        <ListView
            android:id="@+id/listSelector"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></ListView>


    </LinearLayout>

</LinearLayout>

和ListView项目是:

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


    <RelativeLayout
        android:id="@+id/layoutSelector"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@android:color/darker_gray">


        <TextView
            android:id="@+id/txtItemName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/txtValueChanges"
            android:padding="5dp"
            android:text="Item Name"
            android:textAppearance="?android:textAppearanceLarge"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/txtValueChanges"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:visibility="gone"
            android:text="0"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />


    </RelativeLayout>


</LinearLayout>

现在这是我的mailActivity类看起来像:

    public class ListActivitys extends Activity {

    ArrayList listItemName;
    public static ListView listSelector;
    SeekBar seekValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        listItemName = new ArrayList();



        for (int i = 0; i < 50; i++) {
            listItemName.add("Item : " + (i + 1));
        }

        seekValue = (SeekBar) findViewById(R.id.seekValue);
        listSelector = (ListView) findViewById(R.id.listSelector);
        listSelector.setAdapter(new AdapterList(ListActivitys.this, listItemName,seekValue));


    }


}

最后我的自定义适配器是:

    public class AdapterList extends BaseAdapter {

    Context mcContext;
    ArrayList listItems, TempListItems;
    int selectedPos = 0;
    SeekBar seekValue;


    public AdapterList(Context listActivitys, ArrayList listItemName, SeekBar seek) {
        listItems = listItemName;
        mcContext = listActivitys;
        seekValue = seek;


        createATemList(listItemName.size());
    }

    private void createATemList(int size) {
        TempListItems = new ArrayList();

        for (int i=0; i<size; i++){
            TempListItems.add("0");
        }
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        LayoutInflater mInflater = (LayoutInflater) mcContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_list_item, null);
            holder = new ViewHolder();
            holder.txtItemName = (TextView) convertView.findViewById(R.id.txtItemName);
            holder.txtValueChanges = (TextView) convertView.findViewById(R.id.txtValueChanges);
            holder.layoutSelector = (RelativeLayout) convertView.findViewById(R.id.layoutSelector);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (selectedPos == position) {
            holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.colorAccent));
        } else {
            holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.transparent));
        }

        if (!TempListItems.get(selectedPos).equals("0")){
            holder.txtValueChanges.setVisibility(View.VISIBLE);
        }else{
            holder.txtValueChanges.setVisibility(View.GONE);
        }

        holder.txtItemName.setText(listItems.get(position).toString());
        holder.layoutSelector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPos = position;
                notifyDataSetChanged();
            }
        });


        seekValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                /*View view = (View) seekBar.getParent();
                if(view != null){
                    TextView tvProgress = (TextView)view.findViewById(R.id.txtValueChanges);
                    tvProgress.setVisibility(View.VISIBLE);
                    tvProgress.setText(progress + "%");
                }*/

                updateView(selectedPos, progress);
            }


            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        return convertView;
    }


    private void updateView(int selectedPos, int progress) {
        View v = ListActivitys.listSelector.getChildAt(selectedPos -
                ListActivitys.listSelector.getFirstVisiblePosition());

        if (v == null)
            return;


        TextView txtValueChanges = (TextView) v.findViewById(R.id.txtValueChanges);
        TempListItems.set(selectedPos,progress+"");
        if (!TempListItems.get(selectedPos).equals("0")){
            System.out.println("=== Progress : " + progress);
            txtValueChanges.setText(TempListItems.get(selectedPos)+"");
            notifyDataSetChanged();
        }
    }



    private class ViewHolder {
        TextView txtValueChanges;
        TextView txtItemName;
        RelativeLayout layoutSelector;
    }
}

当Seekbar值更改时,是否可以更改ListView项的值。如果是的话怎么样?请帮帮我。

感谢。

2 个答案:

答案 0 :(得分:1)

Activity

seekValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // change the adapter datasource
        listItemName.get(adapter.selectedPos) = "value you want to change";  

        // notify update
        adapter.notifyItemChanged(adapter.selectedPos);
    }
}

答案 1 :(得分:1)

我认为你可以在不将搜索栏传递给适配器的情况下完成。

  1. 像这样修改getView():

    if (selectedPos == position) {
        holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.colorAccent));
        // Add the line below
        holder.txtValueChanges.setText(TempListItems.get(selectedPos));
    } else {
        holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.transparent));
    }
    
  2. 更改updateView()[注意:它是公共的,而不是私有的],如下所示:

    public void updateView(int progress) {
        TempListItems.set(selectedPos, progress+"");
        System.out.println("=== Progress : " + progress);
        notifyDataSetChanged();
    }
    
  3. 将OnSeekbarChangeListener()移回Activity onCreate()。

  4. 像这样修改OnSeekbarChangeListener():

    seekValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            AdapterList tmpAdapter = (AdapterList) listSelector.getAdapter();
            tmpAdapter.updateView(progress);
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}
    });
    
  5. 希望这有帮助!