android自定义控件自动换行效果实现
来源:互联网 发布:以太网数据帧结构 编辑:程序博客网 时间:2024/05/16 17:33
第一篇博客里面有介绍一篇关于自动换行实现诸多自定义控件跟各种效果的博文,但是碍于当初技术能力有限,写的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;@Overrideprotected 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() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Toast.makeText(MainActivity.this, "点击了 : "+arg2, 300).show();}});lv.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic 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()){@Overridepublic void handleMessage(Message msg) {try{if(msg.getData().containsKey("showSexangleListView")){ShowSexangleListView();}}catch(Exception e){}}};public class MyFormatListThread implements Runnable{@Overridepublic 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);}}}@Overridepublic 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[]> {@Overrideprotected String[] doInBackground(Void... params) {// Simulates a background job.try {Thread.sleep(1000);if(list == null)list = new ArrayList<String>();elselist.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;}@Overrideprotected 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);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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
- android自定义控件自动换行效果实现
- android自定义控件自动换行效果实现
- android自定义控件实现自动换行效果
- android自定义控件自动换行效果实现
- Android 自定义自动换行控件
- 自定义布局控件,实现lineaerlayout自动换行
- Android自定义ViewGroup自动换行实现滑动任意布局及事件处理效果
- Android自定义控件实现标签的显示自动换行(一)
- Android 控件自动换行
- Android 控件自动换行
- Android 自定义控件-自动换行的流线性布局-DragFlowLayout
- Android:实现类似RadioButton自动换行及单选效果
- Android 自定义控件实现刮刮卡效果
- Android自定义控件简单实现ratingbar效果
- Android自定义控件实现雷达效果
- android 自定义控件样式点击效果实现
- 自定义布局实现自动换行
- 自定义自动换行布局控件,实现子控件宽度超出整行时自动换行。setMeasuredDimension的使用
- Solr从数据库导入数据
- hadoop安装简介
- How to Deploy or Remove a Solution Package Using PowerShell Commands
- 百度电商要起死回生
- 关于PDB文件和dll调试
- android自定义控件自动换行效果实现
- oracle 11g不能导出空表的解决方法
- 精研Servlet,HttpServlet的实现追究
- 深入理解ARM跳转指令B以及在此基础上理解arm-linux中断向量表中的内容。
- 基于visual Studio2013解决C语言竞赛题之1089牛虎过河
- opencv例2.3实现视频播放(带滚动条)
- Visual C++ 6.0 常见断言错误总结
- 一次开发ios rsa的过程
- android intent 传递对象需要序列化实现Parcelable接口