我想以粗体显示原始文本,其中选中了复选框

时间:2015-11-30 14:13:05

标签: android

我想在原始中以粗体显示文本,其中选中了复选框。例如,如果选中第二个复选框,则将文本更改为粗体,这在我的代码中有效但当我滚动listview时,randmoly字体在其他原始文件中变为粗体,我是如何解决此问题的。

这是我的代码。

FilterActivity.java

public class FilterActivity extends Activity {

    ListView lstfilter;

    ImageView next, prev;
    public static ArrayList<String> mylist;
    public static ArrayList<String> mycategorylist;
    HorizontalScrollView horizontalScroll;
    HorizontalScrollAdapter horizonAdapter;
    Boolean flag = false;
    private HorizontalListView mHlvCustomListWithDividerAndFadingEdge;
    LinearLayout hlayout;
    ArrayList<Integer> array_image = new ArrayList<Integer>();
    MyAdapter adapter1;
    private static LayoutInflater inflater = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        mylist = new ArrayList<String>();
        mycategorylist = new ArrayList<String>();


        mHlvCustomListWithDividerAndFadingEdge = (HorizontalListView) findViewById(R.id.hlvCustomListWithDividerAndFadingEdge);
        hlayout = (LinearLayout) findViewById(R.id.horizontal_menu);
        prev = (ImageView) findViewById(R.id.imgprev);
        next = (ImageView) findViewById(R.id.imgnext);
        lstfilter = (ListView) findViewById(R.id.lstfilter);

        mHlvCustomListWithDividerAndFadingEdge.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                Log.d("in on touch method","");
                return false;
            }
        });

        mylist.add("Rs.251 n Below(253 result found)");
        mylist.add("Rs.502 - 750(25301 result found))");
        mylist.add("Rs.503 - 750(25301 result found))");
        mylist.add("Rs.504 - 750(25301 result found))");
        mylist.add("Rs.505 - 750(25301 result found))");
        mylist.add("Rs.506 - 750(25301 result found))");
        mylist.add("Rs.507 - 750(25301 result found))");
        mylist.add("Rs.508 - 750(25301 result found))");
        mylist.add("Rs.509 - 750(25301 result found))");
        mylist.add("Rs.510 - 750(25301 result found))");
        mylist.add("Rs.511 - 750(25301 result found))");
        mylist.add("Rs.509 - 750(25301 result found))");
        mylist.add("Rs.510 - 750(25301 result found))");
        mylist.add("Rs.511 - 750(25301 result found))");
        mylist.add("Rs.512 - 750(25301 result found))");
        mylist.add("Rs.513 - 750(25301 result found))");
        mylist.add("Rs.514 - 750(25301 result found))");
        mylist.add("Rs.515 - 750(25301 result found))");
        mylist.add("Rs.516 - 750(25301 result found))");
        mylist.add("Rs.517 - 750(25301 result found))");

         CustomData[] mCustomData = new CustomData[] {
                    new CustomData("Brand", R.drawable.img_brand, R.drawable.backgrounimage),
                    new CustomData("Price", R.drawable.img_rupee,R.drawable.img_white_rect),
                    new CustomData("Discount", R.drawable.img_discount,R.drawable.backgrounimage),
                    new CustomData("Availability", R.drawable.yellow_check_mark,R.drawable.backgrounimage),
                    new CustomData("More", R.drawable.backgrounimage,R.drawable.backgrounimage),

            };

        array_image.add(R.drawable.img_brand);
        array_image.add(R.drawable.img_rupee);
        array_image.add(R.drawable.img_discount);
        array_image.add(R.drawable.yellow_check_mark);
        array_image.add(R.drawable.yellow_check_mark);
         CustomArrayAdapter adapter = new CustomArrayAdapter(this, mCustomData);
           mHlvCustomListWithDividerAndFadingEdge.setAdapter(adapter);

        // Getting adapter by passing xml data ArrayList
        prev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                mHlvCustomListWithDividerAndFadingEdge = (HorizontalListView) findViewById(R.id.hlvCustomListWithDividerAndFadingEdge);
                mHlvCustomListWithDividerAndFadingEdge.scrollTo(-500);
                flag = true;
                if (flag) {
                    next.setVisibility(ImageView.VISIBLE);
                    prev.setVisibility(ImageView.GONE);
                }
                else
                {
                    next.setVisibility(ImageView.GONE);
                    prev.setVisibility(ImageView.VISIBLE);
                }
//              int firstVisibleItem = mHlvCustomListWithDividerAndFadingEdge
//                      .getFirstVisiblePosition();
//              int lastVisibleCount = mHlvCustomListWithDividerAndFadingEdge
//                      .getLastVisiblePosition();
//              if (firstVisibleItem != 0) {
//                  int count1 = firstVisibleItem
//                          - (lastVisibleCount - firstVisibleItem);
//                  if (count1 < 0) {
//                      mHlvCustomListWithDividerAndFadingEdge.setSelection(0);
//                      prev.setVisibility(ImageView.VISIBLE);
//
//                  }
//
//                  else {
//                      mHlvCustomListWithDividerAndFadingEdge.setSelection(count1);
//                      prev.setVisibility(ImageView.GONE);
//
//                  }
//                  Log.d("item selected:", "" + count1);
////                    if (count1 == 0) {
////                        Log.d("first item visible", "");
////                        prev.setVisibility(ImageView.GONE);
////                        next.setVisibility(ImageView.VISIBLE);
////
////                    } else {
////                        prev.setVisibility(ImageView.VISIBLE);
////                        next.setVisibility(ImageView.GONE);
////                    }
//              }
////
            }
        });

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // horizontalScroll.scrollTo(
                // (int) horizontalScroll.getScrollX() + 10,
                // (int) horizontalScroll.getScrollY());

                mHlvCustomListWithDividerAndFadingEdge = (HorizontalListView) findViewById(R.id.hlvCustomListWithDividerAndFadingEdge);
                mHlvCustomListWithDividerAndFadingEdge.scrollTo(500);
                flag = true;
                if (flag) {
                    next.setVisibility(ImageView.GONE);
                    prev.setVisibility(ImageView.VISIBLE);
                }
                else
                {
                    next.setVisibility(ImageView.VISIBLE);
                    prev.setVisibility(ImageView.GONE);
                }
//              int count = mHlvCustomListWithDividerAndFadingEdge.getCount();
//              int firstVisibleItem = mHlvCustomListWithDividerAndFadingEdge
//                      .getFirstVisiblePosition();
//              int lastVisibleCount = mHlvCustomListWithDividerAndFadingEdge
//                      .getLastVisiblePosition();
//              if (lastVisibleCount != count) {
//                  
//                  int count1 = lastVisibleCount
//                          + (lastVisibleCount - firstVisibleItem);
//                  if (count1 > count) {
//                      mHlvCustomListWithDividerAndFadingEdge.setSelection(count);
//                      next.setVisibility(ImageView.VISIBLE);
//
//                  } else {
//                      mHlvCustomListWithDividerAndFadingEdge.setSelection(count1);
//                      next.setVisibility(ImageView.GONE);
//
//                  }
//                  Log.d("item selected:", "" + lastVisibleCount);
////                    if (count1 == lastVisibleCount) {
////                        Log.d("last item visible", "");
////                        prev.setVisibility(ImageView.VISIBLE);
////                        next.setVisibility(ImageView.GONE);
////
////                    } else {
////                        prev.setVisibility(ImageView.VISIBLE);
////                        next.setVisibility(ImageView.GONE);
////                    }
//              }
//
        }

        });

        adapter1 = new MyAdapter(this, mylist);
        lstfilter.setAdapter(adapter1);

    }

}
Myadapter
public class MyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<String> data;
    private static LayoutInflater inflater = null;
    public ArrayList<Boolean> positionArray;
     private MyAdapter mListAdapter;  
    // public ImageLoader imageLoader;

    public MyAdapter(Activity a, ArrayList<String> list) {
        activity = a;
        data = list;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        positionArray = new ArrayList<Boolean>(list.size());
        for (int i = 0; i < list.size(); i++) {
            positionArray.add(false);
        }

        // imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

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

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

    // public View getView(int position, View convertView, ViewGroup parent) {
    // Holder holder;
    // View vi = convertView;
    // if (convertView == null) {
    // vi = inflater.inflate(R.layout.activity_filter_list_item, null);
    // holder = new Holder(vi);
    // vi.setTag(holder);
    // holder.txvfirst = (TextView) vi.findViewById(R.id.txv1);
    // holder.chkfirst = (CheckBox) vi.findViewById(R.id.chk1);
    // holder.txvfirst.setTypeface(null, Typeface.BOLD);
    // holder.txvfirst.setText(data.get(position));
    // vi.setTag(R.id.txv1, holder.txvfirst);
    // vi.setTag(R.id.chk1, holder.chkfirst);
    //
    // } else {
    // holder = (Holder) convertView.getTag();
    // holder.txvfirst = (TextView)vi.getTag(R.id.txv1);
    // holder.chkfirst = (CheckBox)vi.getTag(R.id.chk1);
    // }
    // return vi;
    // }
    //
    // private static class Holder {
    // public TextView txvfirst;
    // public CheckBox chkfirst;
    // public Holder(View item) {
    // txvfirst = (TextView) item.findViewById(R.id.txv1);
    // chkfirst = (CheckBox) item.findViewById(R.id.chk1);
    //
    // }
    //
    // }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //final Holder holder;
         View vi = convertView;
            if (vi == null)
                vi = inflater.inflate(R.layout.activity_filter_list_item,
                        null);

          final TextView  txvfirst = (TextView) vi.findViewById(R.id.txv1);
         CheckBox   chkfirst = (CheckBox) vi.findViewById(R.id.chk1);
         mListAdapter = new MyAdapter(activity, data);
         mListAdapter.notifyDataSetChanged();

            chkfirst.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        positionArray.set(position, true);
                        Log.d("textview bold position:",""+position);
                        txvfirst.setTypeface(null, Typeface.BOLD);
                    } else {
                        positionArray.set(position, false);
                    }
                }
            });
            chkfirst.setChecked(positionArray.get(position));
            txvfirst.setText(data.get(position));



            return vi;
    }

}
activity_filter

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:widget="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res/com.example.ecommerce"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#0088cc"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.10" />

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:background="@null"
            android:scaleType="fitCenter" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25" />

        <ImageButton
            android:id="@+id/imgDeliveryBack"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.28"
            android:background="@null"
            android:padding="12dp"
            android:scaleType="fitCenter"
            android:src="@drawable/img_back_arrow" />

        <TextView
            android:id="@+id/txvdeliveryheader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            android:fontFamily="Roboto regular"
            android:gravity="left|center"
            android:text="@string/header_filters"
            android:textColor="#f8f8f8"
            android:textSize="18sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".9"
            android:gravity="left|center"
            android:text="@null"
            android:textColor="#ffffff" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imgprev"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.05"
            android:background="#3374a3"
            android:padding="5dp"
            android:visibility="gone"
            android:src="@drawable/img_back_arrow" />

        <LinearLayout
            android:id="@+id/horizontal_menu"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="#168fcc"
            android:orientation="horizontal" >

            <com.example.ecommerce.HorizontalListView
                android:id="@+id/hlvCustomListWithDividerAndFadingEdge"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0088cc"
                android:divider="#066da1"
                android:fadingEdgeLength="5dp"
                android:requiresFadingEdge="horizontal" />
        </LinearLayout>

        <ImageView
            android:id="@+id/imgnext"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.05"
            android:background="#3374a3"
            android:padding="5dp"
            android:src="@drawable/img_next" />
    </LinearLayout>

    <ListView
        android:id="@+id/lstfilter"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0.3"
        android:background="#ffffff"
        android:dividerHeight="1dp"
        android:padding="8dp" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="0.5" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#035f8d" >

                <TextView
                    android:id="@+id/txvremove"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:padding="5dp"
                    android:src="@drawable/icon_trash_white"
                    android:text="@string/clear_filters"
                    android:textColor="#ffffff"
                    android:textSize="12sp" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="0.5" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#000000" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:padding="5dp"
                    android:src="@drawable/icon_trash_white"
                    android:text="@string/filter_apply"
                    android:textColor="#ffffff"
                    android:textSize="12sp" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为StateListDrawables一旦尝试就会解决这个问题。我正在使用它来改变所选选项的状态(fontsize,textsize,background)。