android自定义控件自动换行效果实现

     第一篇博客里面有介绍一篇关于自动换行实现诸多自定义控件跟各种效果的博文,但是碍于当初技术能力有限,写的jar包里的代码乱七八糟,在最近忙完了手头的工作,不经意间翻看了之前的代码,真是惨不忍睹,随决定重新封装。重新编写的android-custom-vg前后修改了多次版本暂定为2.6.0,模拟的ListView加载数据的流程,为了更方便的使用。只做了最基础的效果,ListView中定位某个View显示的属性,则没有被实现。

      自定义控件换行效果jar调用API:

       setDividerWidth(int   dividerWidth);  //设置两个Item的水平间距,第一个Item距离左边框距离为0

       setDividerHeight(int   dividerHeight); //设置两个Item的垂直间距,第一行Item距离顶部边框距离为0

       setOnItemClickListener(OnItemClickListener   listener);  //获取Item的点击事件

       setOnItemLongClickListener(OnItemLongClickListener   listener); //获取Item的长按事件

       

       效果图:

                       

 1、activity_main.xml:为了实现滑动效果,把自定义控件放进ScrollView控件中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFF"
        android:fadingEdge="none"
        android:fillViewport="true"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|left"
            android:orientation="vertical" >

            <com.custom.vg.list.CustomListView
                android:id="@+id/sexangleView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />

        <Button
            android:id="@+id/btn_remove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />
    </LinearLayout>

</RelativeLayout>

 2、MainActivity.java

import com.custom.vg.list.CustomListView;
import com.custom.vg.list.OnItemClickListener;
import com.custom.vg.list.OnItemLongClickListener;

public class MainActivity extends Activity implements OnClickListener {
	
	private String TAG = MainActivity.class.getSimpleName();
	private CustomListView lv;
	private List<String> list = null;
	private MainSexangleAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		lv = (CustomListView) findViewById(R.id.sexangleView);
		findViewById(R.id.btn_add).setOnClickListener(this);
		findViewById(R.id.btn_remove).setOnClickListener(this);	
		
		new Thread(new MyFormatListThread()).start();		
	}
	
	private void ShowSexangleListView(){
		
		adapter = new MainSexangleAdapter(this, list);
		lv.setDividerHeight(10);
		lv.setDividerWidth(10);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Toast.makeText(MainActivity.this, "点击了 : "+arg2, 300).show();
			}
		});
		
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				Toast.makeText(MainActivity.this, "Long点击了 : "+arg2, 300).show();
				return true;
			}
		});
	}
	
	private Handler handler = new Handler(Looper.getMainLooper()){

		@Override
		public void handleMessage(Message msg) {
			try{
				if(msg.getData().containsKey("showSexangleListView")){
					ShowSexangleListView();
				}
			}catch(Exception e){}
		}
		
	};
	
	public class MyFormatListThread implements Runnable{

		@Override
		public void run() {
			Bundle b = new Bundle();
			try{
				list = new ArrayList<String>();
				list.add("宇宙");
				list.add("宙即我心");
				list.add("心我心即宇");
				list.add("宇宙即我心我心即");
				list.add("心即宇宙细");
				list.add("即");
				list.add("宇宙细微");
				list.add("祥渊源");
				list.add("之发祥渊源实为");
				list.add("文明之");
				list.add("发祥");
				list.add("源实为诸人种");
				list.add("宇宙");
				list.add("宙即我心");
				list.add("心我心即宇");
				list.add("宇宙即我心我心即");
				list.add("心即宇宙细");
				list.add("即");
				list.add("宇宙细微");
				list.add("祥渊源");
				list.add("之发祥渊源实为");
				list.add("文明之");
				list.add("发祥");
				list.add("源实为诸人种");
				
				b.putBoolean("showSexangleListView", true);
			}catch(Exception e){
				Log.w(TAG, e);
			}finally{
				sendMsgHandler(handler,b);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
	
			list.add("Button添加"+list.size());
			adapter.notifyDataSetChanged();			
			break;

		case R.id.btn_remove:

			list.remove(0);
			adapter.notifyDataSetChanged();
			break;
		}
		
	}
	
	private void sendMsgHandler(Handler handler,Bundle bundle){
		Message msg = handler.obtainMessage();
		msg.setData(bundle);
		handler.sendMessage(msg);
	}
	
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(1000);
				if(list == null)
					list = new ArrayList<String>();
				else
					list.clear();
				list.add("宇宙");
				list.add("宙即我心");
				list.add("心我心即宇");
				list.add("宇宙即我心我心即");
				list.add("心即宇宙细");
				list.add("即");
				list.add("宇宙细微");
				list.add("祥渊源");
				list.add("之发祥渊源实为");
				list.add("文明之");
				list.add("发祥");
				list.add("源实为诸人种");
				list.add("宇宙");
				list.add("宙即我心");
				list.add("心我心即宇");
				list.add("宇宙即我心我心即");
				list.add("心即宇宙细");
				list.add("即");
				list.add("宇宙细微");
				list.add("祥渊源");
				list.add("之发祥渊源实为");
				list.add("文明之");
				list.add("发祥");
				list.add("源实为诸人种");
			} catch (InterruptedException e) {
			}
			return null;
		}

		@Override
		protected void onPostExecute(String[] result) {
			// Do some stuff here

			// Call onRefreshComplete when the list has been refreshed.
			ShowSexangleListView();

			super.onPostExecute(result);
		}
	}

}

3、MainSexangleAdapter:继承自CustomAdapter

import com.custom.vg.list.CustomAdapter;

public class MainSexangleAdapter extends CustomAdapter {
	
	private List<String> list;
	private Context con;
	private LayoutInflater inflater;

	public MainSexangleAdapter(Context context, List<String> list) {
		this.con = context;
		this.list = list;
		inflater = LayoutInflater.from(con);
	}

	@Override
	public int getCount() {
		return list.size();
	}

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

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder vh = null;
		if(convertView == null){
			vh = new ViewHolder();
			
			convertView = inflater.inflate(R.layout.adapter_sexangle_item_style, null);
			
			vh.tv = (TextView) convertView.findViewById(R.id.adapter_text);
			
			convertView.setTag(vh);
		}else{
			vh = (ViewHolder) convertView.getTag();
		}
		
		String str = list.get(position);
		vh.tv.setText(str);
		
		return convertView;
	}
	
	public class ViewHolder{
		public TextView tv;
	}

}

基本调用方法跟ListView模式很近,Item可以自定义任意界面,不局限于上图。如需对Item操作显示不同效果也可在Adapter中进行处理,修改完List集合中的数据然后adapter刷新操作即可。


需在项目中导入android-custom-vg2.6.0.jar包

下载地址:

http://download.csdn.net/detail/alovebtoc/6691871





  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 37
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值