android自定义控件自动换行效果实现
来源:互联网 发布:中田健身工作室 知乎 编辑:程序博客网 时间:2024/05/16 18:35
转自别人的文章,自己花时间做了优化写了demo,不想一点点写demo的可以在后面自己下我的demo
修改并简化后的demo下载地址 http://download.csdn.net/detail/wjyyxzzjnws/8563581
第一篇博客里面有介绍一篇关于自动换行实现诸多自定义控件跟各种效果的博文,但是碍于当初技术能力有限,写的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刷新操作即可。
demo 下载:http://download.csdn.net/detail/wjyyxzzjnws/8563549
- android自定义控件自动换行效果实现
- android自定义控件自动换行效果实现
- android自定义控件实现自动换行效果
- android自定义控件自动换行效果实现
- Android 自定义自动换行控件
- 自定义布局控件,实现lineaerlayout自动换行
- Android自定义ViewGroup自动换行实现滑动任意布局及事件处理效果
- Android自定义控件实现标签的显示自动换行(一)
- Android 控件自动换行
- Android 控件自动换行
- Android 自定义控件-自动换行的流线性布局-DragFlowLayout
- Android:实现类似RadioButton自动换行及单选效果
- Android 自定义控件实现刮刮卡效果
- Android自定义控件简单实现ratingbar效果
- Android自定义控件实现雷达效果
- android 自定义控件样式点击效果实现
- 自定义布局实现自动换行
- 自定义自动换行布局控件,实现子控件宽度超出整行时自动换行。setMeasuredDimension的使用
- 高精度模板
- PKU 1562 Oil Deposit
- 在Linux下使用vim配合xxd查看并编辑二进制文件
- itoa函数的使用与说明
- 第39级台阶
- android自定义控件自动换行效果实现
- c++常见问题总结
- 正则去掉html
- 应用内跳转到设置相关页面
- GIT学习日记二:Git版本回退
- Objective-C—autorelease、ARC、block、protocol(协议)
- 封装一个类搞定90%安卓客户端与服务器端交互
- Android ViewPager实现广告条效果
- Foundation框架—结构体、NSString