android 布局 View初始化代码自动生成

在android开发过程中,界面布局是及其重要的,但同时也是复杂。有的时候我们急于实际运行查看布局效果。但是android的编译速度我实在不想吐槽啥,尤其在布局越来越复杂,项目越来越大,资源文件越来越多的情况下。尤其是是android的view的初始化,findViewbyId 完全是体力活,我们完全可以根据布局文件自动生成View的初始化代码。首先声明:1.这是及其容易做到的,实用性
摘要由CSDN通过智能技术生成

在android开发过程中,界面布局是及其重要的,但同时也是复杂。有的时候我们急于实际运行查看布局效果。但是android的编译速度我实在不想吐槽啥,尤其在布局越来越复杂,项目越来越大,资源文件越来越多的情况下。

尤其是是android的view的初始化,findViewbyId 完全是体力活,我们完全可以根据布局文件自动生成View的初始化代码。

首先声明:

1.这是及其容易做到的,实用性性一般,但是在复杂布局和首次写初始化View代码的时候及其好用。

2.只能生成有id标签的view的初始化代码。

思路

其实很简单,就是解析layout 布局文件,把有id属性的标签的一些信息(标签类型,id名称等)存起来,然后根据这些信息生成固定的代码。

实现

直接上代码,首先是对layout文件的解析,把解析出来的信息放在一个list中

 

public class SaxHander extends DefaultHandler {
	private List<IdNamePair> map = new ArrayList<IdNamePair>();

	
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		map.clear();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri,
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个简单的实现示例,该示例使用SharedPreferences来存储已收藏的数据: 1. 在listview的item布局文件中添加一个收藏按钮。 ```xml <Button android:id="@+id/btn_favorite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_favorite_border" android:focusable="false" android:focusableInTouchMode="false" android:clickable="true" /> ``` 2. 在listview的adapter中,为每个收藏按钮添加点击事件监听器,并在监听器中将该item的数据存储到本地。 ```java public class MyAdapter extends BaseAdapter { private List<Item> itemList; private Context context; private SharedPreferences sharedPreferences; public MyAdapter(List<Item> itemList, Context context) { this.itemList = itemList; this.context = context; sharedPreferences = context.getSharedPreferences("favorites", Context.MODE_PRIVATE); } @Override public View getView(final int position, View convertView, ViewGroup parent) { // ... Button btnFavorite = convertView.findViewById(R.id.btn_favorite); btnFavorite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取当前item的数据 Item item = itemList.get(position); // 将数据存储到SharedPreferences中 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(item.getId(), item.toJson()); editor.apply(); // 切换收藏按钮的背景 Button btn = (Button) v; btn.setBackgroundResource(R.drawable.ic_favorite); btn.setTag("1"); } }); // ... } } ``` 3. 当用户再次进入该页面时,从本地读取已收藏的数据,并将其显示在listview中。 ```java public class MyActivity extends AppCompatActivity { private ListView listView; private List<Item> itemList; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); // 初始化listview listView = findViewById(R.id.listview); // 获取本地已收藏的数据 sharedPreferences = getSharedPreferences("favorites", Context.MODE_PRIVATE); Map<String, ?> map = sharedPreferences.getAll(); itemList = new ArrayList<>(); for (Map.Entry<String, ?> entry : map.entrySet()) { Item item = Item.fromJson((String) entry.getValue()); itemList.add(item); } // 设置listview的adapter MyAdapter adapter = new MyAdapter(itemList, this); listView.setAdapter(adapter); } } ``` 4. 在listview的adapter中,为已收藏的item设置不同的背景或者图标,以便用户可以快速区分已收藏的item。 ```java public class MyAdapter extends BaseAdapter { private List<Item> itemList; private Context context; private SharedPreferences sharedPreferences; public MyAdapter(List<Item> itemList, Context context) { this.itemList = itemList; this.context = context; sharedPreferences = context.getSharedPreferences("favorites", Context.MODE_PRIVATE); } @Override public View getView(final int position, View convertView, ViewGroup parent) { // ... Button btnFavorite = convertView.findViewById(R.id.btn_favorite); // 判断当前item是否已收藏 if (sharedPreferences.contains(item.getId())) { btnFavorite.setBackgroundResource(R.drawable.ic_favorite); btnFavorite.setTag("1"); } else { btnFavorite.setBackgroundResource(R.drawable.ic_favorite_border); btnFavorite.setTag("0"); } btnFavorite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ... } }); // ... } } ``` 5. 当用户取消收藏时,从本地删除该item的数据,并将其从listview中删除。 ```java public class MyAdapter extends BaseAdapter { private List<Item> itemList; private Context context; private SharedPreferences sharedPreferences; public MyAdapter(List<Item> itemList, Context context) { this.itemList = itemList; this.context = context; sharedPreferences = context.getSharedPreferences("favorites", Context.MODE_PRIVATE); } @Override public View getView(final int position, View convertView, ViewGroup parent) { // ... Button btnFavorite = convertView.findViewById(R.id.btn_favorite); // 判断当前item是否已收藏 if (sharedPreferences.contains(item.getId())) { btnFavorite.setBackgroundResource(R.drawable.ic_favorite); btnFavorite.setTag("1"); } else { btnFavorite.setBackgroundResource(R.drawable.ic_favorite_border); btnFavorite.setTag("0"); } btnFavorite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button btn = (Button) v; if ("0".equals(btn.getTag())) { // 添加收藏 Item item = itemList.get(position); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(item.getId(), item.toJson()); editor.apply(); btn.setBackgroundResource(R.drawable.ic_favorite); btn.setTag("1"); } else { // 取消收藏 Item item = itemList.get(position); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.remove(item.getId()); editor.apply(); btn.setBackgroundResource(R.drawable.ic_favorite_border); btn.setTag("0"); } } }); // ... } } ``` 以上是一个简单的实现示例,你可以根据实际需求进行调整。注:这里的Item类是自定义的,你可以根据实际需求替换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值