java - 可以更改 CustomListview 背景样式吗?

标签 java android listview

我正在尝试开发一个Android应用程序。我对数组使用了自定义 ListView 。

我有 4 个这样的按钮:

i have 4 buttons like that :

但我想要:当黄色按钮单击 ListView 的样式时:

When button 1 clicked Listview's style :

当按钮2点击listview的样式时::

When Purple button clicked

我的代码如下:

自定义 TextView 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"/>

自定义适配器.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;} }

最佳答案

示例代码如下:

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)”是一个按钮点击事件,希望对您有帮助。

关于java - 可以更改 CustomListview 背景样式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38143923/

相关文章:

JavaFx 可运行 JAR 导出不起作用

android - Gradle Android - 覆盖标准任务

android - SharedPreferences:应用程序崩溃时如何清除所有首选项

json - 如何在Flutter ListView构建期间测试json属性是否存在

c# - WPF ListView 虚拟化。如何禁用 ListView 虚拟化?

java - Hadoop 2.2.0 源码打包

java - netty(或java NIO) channel 是先进先出的吗?

java - 有没有一种方法可以将文件加载到java中的多个类中,而不必每次都调用它们?

android - Appcompact DialogFragment单选复选标记颜色

Android:只有我的 ListView 的最后一行对 OnClickListener 使用react