我们自由开发软件决定因素在于用户,如何使用户方便快捷的使用软件,这个是首要的,之前有的用户看到其他的直播软件能够在直播播放界面能够切换频道,很多人就提出这个需求,我看了一下这个功能其实也是很简单的,可能很多Android开发者也做过就是使用PopupWindow+ListView+电视源,所以我们就添加了这个功能,效果如下:
如何实现上面的效果呢,首先我们写一个布局文件
pop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/turnk"
android:orientation="vertical" >
<ListView
android:id="@+id/pop_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0.0dip"
android:background="#082f2f2f"
android:cacheColorHint="#00000000"
android:divider="@drawable/keke_divider"
android:fadeScrollbars="false"
android:fastScrollEnabled="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="none"
android:scrollingCache="false" />
</LinearLayout>
然后就是ListView的Item布局文件
pop_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/pop_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="3dip"
android:textSize="15sp" />
<TextView
android:id="@+id/pop_channel_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dip"
android:maxLines="2"
android:textSize="15sp" />
</LinearLayout>
接下来我们就需要编写一个PopupWindow
// 创建一个包含自定义ListView的PopupWindow
private PopupWindow makePopupWindow(Context cx) {
PopupWindow window;
window = new PopupWindow(cx);
ItemAdapter apAdapter = new ItemAdapter(cx, listItems);
View view = LayoutInflater.from(cx).inflate(R.layout.togic_select_pop,
null);
window.setContentView(view);
ListView listView = (ListView) view.findViewById(R.id.togic_pop_list);
listView.setAdapter(apAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
currentChannelPos = position;
CheckChannel();
}
});
window.setWidth(dip2px(150));
window.setHeight(dip2px(230));
// 设置PopupWindow外部区域是否可触
window.setFocusable(true);
window.setTouchable(true);
window.setOutsideTouchable(true);
return window;
}
public void CheckChannel(){
CurrentChannel = listItems.get(currentChannelPos);
mTitle = CurrentChannel.getTitle();
mPath = CurrentChannel.getUrls().get(currentUrlPos);
if (mPath.startsWith("http:")) {
mVideoView.setVideoURI(Uri.parse(mPath));
} else {
mVideoView.setVideoPath(mPath);
}
}
其中上面的listItems就是要传进的频道的列表信息,这个自己定义
接下来就是创建LIstView的Adapter
class ItemAdapter extends BaseAdapter {
public List<KeKeChannelType> listItems;
private LayoutInflater mLayoutInflater;
public ItemAdapter(Context context, List<KeKeChannelType> list) {
mLayoutInflater = (LayoutInflater) context
.getSystemService("layout_inflater");
listItems = list;
}
@Override
public int getCount() {
if (listItems != null) {
return listItems.size();
} else {
return 0;
}
}
@Override
public Object getItem(int position) {
return listItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
view = mLayoutInflater.inflate(pop_item, null);
viewHolder.num = (TextView) view
.findViewById(R.id.pop_num);
viewHolder.channel_name = (TextView) view
.findViewById(R.id.pop_channel_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
ChannelType channelType = listItems.get(position);
viewHolder.num.setText(channelType.getNum() + "");
viewHolder.channel_name.setText(channelType.getTitle());
return view;
}
class ViewHolder {
TextView num;
TextView channel_name;
}
}
然后在你的播放界面上添加一个触发事件让Pop来实现就可以了
PopupWindow pop = makePopupWindow(mContext);
pop.showAtLocation(Parent, Gravity.LEFT|Gravity.TOP, 10, 10);
好了,这就完成了。