篇如题,这篇文章目前我认为很适用于中小型的项目,因为在初、中级程序员的世界中,以下的效果,大多会使用startActivity携带数据跳转新的Activity用于填充ListView或者是RecyclerView,俩者相比的话资源、效果、性能、内存都有所不同,比较下来的话,我们本章的知识更适用于项目中,当然startActivity的展现方式写起来有的朋友认为会更简单,但是当你学会本章知识的话,这类型的问题是通吃的!
此篇博文知识点:
1.ListView的使用方式
http://blog.csdn.net/qq_20451879/article/details/54849260
2.popupWindow的使用方式
http://blog.csdn.net/qq_20451879/article/details/72320325
3.接口回调的基本使用(第三个是为理解本文刚写的)
http://blog.csdn.net/qq_20451879/article/details/60958435
http://blog.csdn.net/qq_20451879/article/details/52117145
http://blog.csdn.net/qq_20451879/article/details/75669871
4.基础自定义控件使用
1.首先创建接口:
作用:记录,传递数据
CustomInterface
package com.yl.popupview.custompopupview;
/**
* Created by YongLiu on 2017/7/20.
*/
public interface CustomInterface {
void setData(String string);
}
2.自定义控件
作用:所弹视图的UI与数据的承载
CustomViewPopup
package com.yl.popupview.custompopupview;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import java.util.ArrayList;
/**
* Created by YongLiu on 2017/7/20.
*/
public class CustomViewPopup extends PopupWindow {
private CustomInterface custominterface;
private Context mContext;
private final PopupAdapter popupAdapter;
private final View view;
public CustomViewPopup(final Context context, CustomInterface custominterface){
/**
* 注意:我们的接口同时作为成员变量传入,因为我们用于监听子Item的数据监听
* */
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.fragment_layout, null);
mContext=context;
this.setContentView(view);
//自定义基础,设置我们显示控件的宽,高,焦点,点击外部关闭PopupWindow操作
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
this.setFocusable(true);
this.setOutsideTouchable(true);
//更新试图
this.update();
//设置背景
ColorDrawable colorDrawable = new ColorDrawable(0000000000);
this.setBackgroundDrawable(colorDrawable);
ListView mList = (ListView) view.findViewById(R.id.lv_popup_view);
//数据填充
ArrayList<String> Lists = new ArrayList<>();
Lists.add("村里第一帅");
Lists.add("镇里第一帅");
Lists.add("市里第一帅");
Lists.add("省里第一帅");
Lists.add("中国第一帅");
Lists.add("世界第一帅");
//绑定适配器,设置子条目监听
popupAdapter = new PopupAdapter(mContext,Lists);
this.custominterface=custominterface;
mList.setAdapter(popupAdapter);
mList.setOnItemClickListener(onItemClickListener);
}
//监听子Item操作 -数据记录,关闭PopupWindow
public AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
custominterface.setData(popupAdapter.getItem(position));
dismiss();
}
};
}
3.Adapter 适配器
作用:数据适配,填充Item样式,数据设置等
PopupAdapter
package com.yl.popupview.custompopupview;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
/**
* Created by YongLiu on 2017/7/21.
*/
public class PopupAdapter extends BaseAdapter {
private List<String> lists;
public Context mContext;
public PopupAdapter(Context context,List lists){
this.mContext=context;
this.lists=lists;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public String getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
convertView= LinearLayout.inflate(mContext,R.layout.item_layout,null);
viewHolder = new ViewHolder();
viewHolder.textView=(TextView) convertView.findViewById(R.id.item_view);
convertView.setTag(viewHolder);
}else{
convertView.getTag();
}
String content = lists.get(position);
if(!TextUtils.isEmpty(content)){
viewHolder.textView.setText(lists.get(position));
}
return convertView;
}
class ViewHolder {
public TextView textView;
}
}
4.最不重要的MainActivity - -
注意:一切的开发都是基于MainActivity的onCreate开发 - 一位CTO所言
package com.yl.popupview.custompopupview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements CustomInterface, View.OnClickListener {
private TextView mBtn;
private TextView mContent;
private CustomViewPopup customViewPopipup;
private TextView mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mBtn.setOnClickListener(this);
}
private void initView() {
mBtn = (TextView) findViewById(R.id.tv_btn);
mContent = (TextView) findViewById(R.id.tv_content);
mTitle = (TextView) findViewById(R.id.tv_title);
}
//实现接口,获取我们PopupWindow内存的数据
@Override
public void setData(String string) {
mContent.setText(string);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_btn:
customViewPopipup = new CustomViewPopup(this,this);
if(customViewPopipup.isShowing()){
customViewPopipup.dismiss();
}else{
//设置我们弹出的PopupWindow的位置,基于某个视图之下
customViewPopipup.showAsDropDown(mTitle);
}
break;
}
}
}
ItemLayout Xml(ListView内的Item样式)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:id="@+id/item_view"
android:textSize="20sp"
/>
RelativeLayout>
后记:来源于一位老程序员的项目知识点,加以学习,完善自身。