可更改的CustomListview背景样式?

时间:2016-07-01 11:10:48

标签: java android listview

我正在尝试开发一款Android应用。我使用Custom listview for array。

我有4个这样的按钮:

i have 4 buttons like that :

但我想这样:当黄色按钮点击listview的风格时:

When button 1 clicked Listview's style :

当Button 2点击listview的样式::

When Purple button clicked

我的代码如下:

CustomTextview xml:     

    android:background="@drawable/lstx_1"
    android:id="@+id/ls_txt"
    android:textSize="20dp"
    android:textAlignment="center"
    android:textColor="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="40dp"/>

CustomAdapter.java

public class customlist extends ArrayAdapter<String>  {
 private final Activity context;
    private final String[] hikayeler;
    private Typeface tf;
public customlist(Activity context, String[] hikayeler) {
        super(context,R.layout.lsv_txt, hikayeler);
        this.context = context;
        this.hikayeler = hikayeler;
        this.tf = Typeface.createFromAsset(context.getAssets(), "poetsen.ttf");
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.lsv_txt, null, true);
        TextView ls_txt = (TextView) rowView.findViewById(R.id.ls_txt);
        ls_txt.setText(hikayeler[position]);
        ls_txt.setTypeface(tf);

        GradientDrawable gdBaslik = new GradientDrawable();
        gdBaslik.setColor(Color.parseColor("#cf96ac"));
        gdBaslik.setCornerRadius(12);
        gdBaslik.setStroke(2, Color.parseColor("#cf96ac"));
        ls_txt.setBackground(gdBaslik);

        return rowView;} }

1 个答案:

答案 0 :(得分:0)

示例代码如下:

private ListView mListView;
	private customlist mcustomAdapter;
	private boolean mStyle = false;

	public void showColor(View v) {
		if (mListView == null) {
			mListView = (ListView) findViewById(R.id.lv_test);
			mcustomAdapter = new customlist(this, new String[] { "aaaa", "bbbbb", "ccccc" });
			mListView.setAdapter(mcustomAdapter);
		} else {
			GradientDrawable gdBaslik = new GradientDrawable();
			if (mStyle) {
				gdBaslik.setColor(Color.parseColor("#cf96ac"));
				gdBaslik.setCornerRadius(12);
				gdBaslik.setStroke(2, Color.parseColor("#cf96ac"));
			} else {
				gdBaslik.setColor(Color.parseColor("#849232"));
				gdBaslik.setCornerRadius(12);
				gdBaslik.setStroke(2, Color.parseColor("#849232"));
			}
			mStyle = !mStyle;
			mcustomAdapter.setGradientDrawable(gdBaslik);
		}
	}

	public class customlist extends ArrayAdapter<String> {
		private final Activity context;
		private final String[] hikayeler;
		private GradientDrawable gdBaslik;
		private Typeface tf;

		public customlist(Activity context, String[] hikayeler) {
			super(context, R.layout.lsv_txt, hikayeler);
			this.context = context;
			this.hikayeler = hikayeler;
			this.tf = Typeface.createFromAsset(context.getAssets(), "poetsen.ttf");
		}

		public void setGradientDrawable(GradientDrawable drawable) {
			this.gdBaslik = drawable;
			notifyDataSetChanged();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = context.getLayoutInflater();
			View rowView = inflater.inflate(R.layout.lsv_txt, null, true);
			TextView ls_txt = (TextView) rowView.findViewById(R.id.ls_txt);
			ls_txt.setText(hikayeler[position]);
			ls_txt.setTypeface(tf);

			ls_txt.setBackground(gdBaslik);

			return rowView;
		}
	}

注意:“showColor(View v)”是一个Button click事件。我希望它可以帮到你。