GithubHelp home page GithubHelp logo

rainbeanfan / android-hyviewinject Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hongyangandroid/android-hyviewinject

0.0 1.0 0.0 1.65 MB

一个Android的ViewInject的注入库,基于编译时注解解析,不会影响性能。

License: Apache License 2.0

Java 100.00%

android-hyviewinject's Introduction

Android-HyViewInject

一个Android的ViewInject的注入库,基于编译时注解的例子。

仅仅是个例子,不要使用的到项目中,如果需要请使用butterknife.

效果图

Sample Screenshots

效果图,第一个Activity中布局文件为TextView和ListView,第二个Activity中为 Fragment,主要测试Activity、Fragment、Adapter中的注入。

用法

In Activity

@InjectView(R.layout.activity_main)
public class MainActivity extends Activity
{
	@InjectView(R.id.id_tv_name)
	public TextView mTvName;
	@InjectView(R.id.id_lv_detail)
	ListView mLvDetails;
	private BeanAdaper mAdapter;
	private List<Bean> mDatas = Bean.generateDatas();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		HyViewInjector.inject(this);

In Fragment

public class SecondActFragment extends Fragment
{
	@InjectView(R.id.id_tv_title)
	public TextView mTitle ; 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_second, container , false);
		HyViewInjector.inject(this, view);
		
		mTitle.setText("I am a title in fragment!");
		return view ; 
	}

}

In Adapter

package com.zhy.sample.viewinjector;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.zhy.sample.viewinjector.bean.Bean;
import com.zhy.util.ioc.HyViewInjector;
import com.zhy.util.ioc.annotation.InjectView;

public class BeanAdaper extends ArrayAdapter<Bean>
{
	public BeanAdaper(Context context, List<Bean> objects)
	{
		super(context, 0, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder = null;
		if (convertView == null)
		{
			convertView = LayoutInflater.from(getContext()).inflate(
					R.layout.item_lv, parent, false);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.mTitle.setText(getItem(position).getTitle());
		holder.mDetail.setText(getItem(position).getDetail());
		return convertView;
	}

	static class ViewHolder
	{
		@InjectView(R.id.id_tv_title)
		TextView mTitle;
		@InjectView(R.id.id_tv_detail)
		TextView mDetail;
		
		public ViewHolder(View view)
		{
			HyViewInjector.inject(this, view);
		}
	}

}

#启用编译时注解 Sample Screenshots 按照上图步骤,启动即可。

#关于我

我的博客地址

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.