时间:2021-05-21
最近遇到一个需求需要圆形listview作为悬浮窗,费了九牛二虎之力终于开发出来了,特别有成就感,下面分享下案例,项目原因,只能分享一部分供大家参考
1.有图有真相
下面就来讲解下代码:
1.自定义listview
2.实体类
import android.graphics.drawable.Drawable;public class Files { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getApp_name() { return app_name; } public void setApp_name(String app_name) { this.app_name = app_name; } public String getPackagename() { return packagename; } public void setPackagename(String packagename) { this.packagename = packagename; } public int getImg_bg_id() { return img_bg_id; } public void setImg_bg_id(int img_bg_id) { this.img_bg_id = img_bg_id; } public boolean isShow() { return isShow; } public void setShow(boolean show) { isShow = show; } private Drawable imgDrawable; public Drawable getImgDrawable() { return imgDrawable; } public void setImgDrawable(Drawable imgDrawable) { this.imgDrawable = imgDrawable; } private boolean isShow; private String app_name; private String packagename; private int img_bg_id;}3.适配器
import android.view.View;import java.util.ArrayList;public abstract class Adapter { private CircleListView circleListView; private ArrayList<Files> allAppList; public Adapter(CircleListView circleListView) { this.circleListView = circleListView; } public int getCount() { return allAppList.size(); } public abstract View getView(int position); public void notifyDataChanged() { circleListView.refreshList(); } public void setPosition(int position) { if (position > getCount() - 1) { position = getCount() - 1; } else if (position < 0) { position = 0; } circleListView.setPosition(position); }}4.float.xml 引用:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/float_btn" android:layout_width="79px" android:layout_height="79px" android:scaleType="fitCenter" android:layout_centerInParent="true" android:background="@drawable/main_selector_floatbtn" android:clickable="true" /> <RelativeLayout android:id="@+id/float_area" android:layout_width="300px" android:layout_height="300px" android:layout_centerInParent="true" android:background="@drawable/float_display_bg" android:visibility="gone"> <com.xinrui.headsettest.circlelistview.CircleListView android:id="@+id/circle_list_view" android:layout_width="300px" android:layout_height="300px" android:layout_centerHorizontal="true"/> <ImageView android:id="@+id/float_main_img" android:layout_width="70px" android:layout_height="70px" android:scaleType="fitCenter" android:layout_centerInParent="true" android:background="@drawable/float_main_icon" android:clickable="true"/> </RelativeLayout></RelativeLayout>5.Activity中应用:
import android.app.Activity;import android.os.Bundle;import android.provider.ContactsContract;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import com.xinrui.headsettest.circlelistview.Adapter;import com.xinrui.headsettest.circlelistview.CircleListView;import com.xinrui.headsettest.circlelistview.Files;import java.util.ArrayList;public class FloatActivity extends Activity implements View.OnClickListener{ private ImageView floatImg, float_main_img; private RelativeLayout float_area; private CircleListView circle_list_view; private Adapter adapter; private ArrayList<Files> appDataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.float_layout); getData(); init(); } private void getData(){ appDataList=new ArrayList<>(); Files files = new Files(); files.setApp_name(this.getResources().getString(R.string.calculator_txt)); files.setImg_bg_id(R.drawable.main_selector_calculator); appDataList.add(files); Files files0 = new Files(); files0.setApp_name(this.getResources().getString(R.string.calendar_txt)); files0.setImg_bg_id(R.drawable.main_selector_calendar); appDataList.add(files0); Files files1 = new Files(); files1.setApp_name(this.getResources().getString(R.string.timer_txt)); files1.setImg_bg_id(R.drawable.main_selector_clock); appDataList.add(files1); Files files2 = new Files(); files2.setApp_name(this.getResources().getString(R.string.vip_txt)); files2.setImg_bg_id(R.drawable.main_selector_vip); appDataList.add(files2); Files files3 = new Files(); files3.setApp_name(this.getResources().getString(R.string.screen_shots_txt)); files3.setImg_bg_id(R.drawable.main_selector_screenshot); appDataList.add(files3); Files files4 = new Files(); files4.setApp_name(this.getResources().getString(R.string.recording_screen_txt)); files4.setImg_bg_id(R.drawable.main_selector_recordingscreen); appDataList.add(files4); } private void init() { float_area = (RelativeLayout) findViewById(R.id.float_area); floatImg = (ImageView) findViewById(R.id.float_btn); float_main_img = (ImageView) findViewById(R.id.float_main_img); float_main_img.bringToFront(); floatImg.setOnClickListener(this); float_main_img.setOnClickListener(this); circle_list_view = findViewById(R.id.circle_list_view); adapter = new Adapter(circle_list_view) { @Override public View getView(final int position) { View view = LayoutInflater.from(FloatActivity.this).inflate(R.layout.item_app,null); TextView app_name = view.findViewById(R.id.app_name); ImageView app_img = view.findViewById(R.id.app_img); ImageView delete_img = view.findViewById(R.id.delete_icon); delete_img.bringToFront(); final Files files = appDataList.get(position); app_name.setText(files.getApp_name()); app_img.setImageResource(files.getImg_bg_id()); if(files.isShow()){ delete_img.setVisibility(View.VISIBLE); }else{ delete_img.setVisibility(View.INVISIBLE); } delete_img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { appDataList.remove(position); Files file = new Files(); file.setShow(false); file.setImg_bg_id(R.drawable.main_selector_additem); file.setApp_name(FloatActivity.this.getResources().getString(R.string.custom_txt)); appDataList.add(position,file); adapter.notifyDataChanged(); } }); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(FloatActivity.this,"my is "+files.getApp_name(),Toast.LENGTH_SHORT).show(); } }); view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if(!files.getApp_name().equals(FloatActivity.this.getResources().getString(R.string.custom_txt))) { for (Files file : appDataList) { if (file.getApp_name().equals(files.getApp_name())) { file.setShow(true); } else { file.setShow(false); } } adapter.notifyDataChanged(); } return true; } }); return view; } @Override public int getCount() { return appDataList.size(); } }; circle_list_view.setAdapter(adapter); adapter.setPosition(0); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.float_main_img: float_area.setVisibility(View.GONE); floatImg.setVisibility(View.VISIBLE); break; case R.id.float_btn: float_area.setVisibility(View.VISIBLE); floatImg.setVisibility(View.GONE); break; } }}大功告成就这样实现了圆形listview…
到此这篇关于Android 实现自定义圆形listview功能的实例代码的文章就介绍到这了,更多相关android 自定义圆形listview内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Android自定义View实现圆形进度条的具体代码,供大家参考,具体内容如下效果如下:主要代码CircularProgressView.j
本文实例讲述了Android自定义圆形进度条,分享给大家供大家参考。具体如下:大家也可以参考这两篇文章进行学习:《自定义Android圆形进度条(附源码)》《A
Android实现自定义圆形进度条:Android自定义view,在大多数项目中根据客户需求及用户的体验度来说,都要重新写控件的来展示漂亮的界面,这里就对圆形进
本文实例为大家分享了Android自定义控件ViewGroup实现标签云的具体代码,供大家参考,具体内容如下实现的功能:基本绘制流程:构造函数获取自定义属性on
本文为大家分享了Android实现拖动选择按钮的具体代码,供大家参考,具体内容如下效果图View代码第一步:自定义属性第二步:自定义圆形publicclassC