码迷,mamicode.com
首页 > 移动开发 > 详细

Android为TV端助力 listview与recyclerview上下联动

时间:2017-12-13 14:53:45      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:dad   lock   private   you   extc   abi   this   schema   coding   

首先是主布局fragment里面的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_reservation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_main_bg"
tools:context="com.hhzt.iptv.lvb_w8.activity.ReservationActivity">


<LinearLayout
android:id="@+id/layout"
android:layout_width="@dimen/layx150"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/title_name"
android:layout_width="match_parent"
android:layout_height="@dimen/layx80"
android:textSize="@dimen/layx30"
android:textColor="@color/white"
android:singleLine="true"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/food_title"
/>

<ListView
android:id="@+id/listview_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="@color/bian_white"
android:dividerHeight="@dimen/layx1"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="true"
android:focusable="true"
android:background="@color/food_content"
android:listSelector="@drawable/food_menu_selector"
android:descendantFocusability="blocksDescendants"
android:scrollbars="none" >
</ListView>

</LinearLayout>

<LinearLayout
android:id="@+id/layout_ll"
android:layout_width="@dimen/layx400"
android:layout_height="@dimen/layx50"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/layx50"
android:layout_marginTop="@dimen/layx50"
android:gravity="center"
>

<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:focusable="true"
android:clickable="true"
android:paddingLeft="@dimen/layx10"
android:paddingRight="@dimen/layx10"
android:background="@drawable/shopping_selector"
>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:padding="@dimen/layx5"
android:src="@drawable/shopping"
/>
<TextView
android:id="@+id/shopping"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="@color/food_d_text"
android:textSize="@dimen/layx28"
android:text="购物车"
/>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:focusable="true"
android:clickable="true"
android:paddingLeft="@dimen/layx10"
android:paddingRight="@dimen/layx10"
android:layout_marginLeft="@dimen/layx20"
android:background="@drawable/shopping_selector"
>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:padding="@dimen/layx5"
android:src="@drawable/history"
/>
<TextView
android:id="@+id/history"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="@color/food_d_text"
android:textSize="@dimen/layx28"
android:text="历史订单"
/>
</LinearLayout>


</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/listview_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/layout"
android:layout_below="@id/layout_ll"
android:paddingLeft="@dimen/layx20"
android:paddingRight="@dimen/layx20"
android:layout_marginTop="@dimen/layx20"
android:nextFocusUp="@+id/shopping"
android:scrollbars="none"/>
</RelativeLayout>


然后是activity上面的fragment

package com.hhzt.iptv.lvb_w8.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.hhzt.iptv.R;
import com.hhzt.iptv.lvb_w8.adapter.FoodAdapter;
import com.hhzt.iptv.lvb_w8.adapter.FoodAdapterRight;
import com.hhzt.iptv.lvb_w8.bean.FoodBean;
import com.hhzt.iptv.lvb_w8.bean.FoodDetailsBean;
import com.hhzt.iptv.lvb_w8.interfaces.IBeanOnSuccessCB;
import com.hhzt.iptv.lvb_y.BaseActivity;
import com.hhzt.iptv.lvb_y.business.UIDataller;
import com.hhzt.iptv.lvb_y.log.LogUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

//订餐服务界面
public class ReservationFragment extends BaseFragment implements FoodAdapterRight.OnItemClickListener{

@ViewInject(R.id.title_name)
private TextView mWelcomeTextView; //标题
@ViewInject(R.id.listview_left)
private ListView mListview_left; //列表list
@ViewInject(R.id.shopping)
private TextView mShopping; //购物车
@ViewInject(R.id.history)
private TextView mHistory; //历史订单
@ViewInject(R.id.listview_right)
private RecyclerView mListViewRight; //菜品详情RecyclerView
private ArrayList<FoodBean.ListBean> mBean;
//详情页的list
private ArrayList<FoodDetailsBean.ListBean> mFoodDetailsBean = new ArrayList<>();

private FoodAdapterRight mAdapterRight;
private String mCurTitle = "";
private int mTitlePos = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_reservation, container, false);
ViewUtils.inject(this, view);
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (null == savedInstanceState) {
requseFoodType();
setMonitor();
}
}

private void setMonitor() {
mListview_left.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_UP:
if(event.getAction() == KeyEvent.ACTION_DOWN){
if(mTitlePos > 0){
mTitlePos --;
mListview_left.setSelection(mTitlePos);
requseFoodDetailedMenu(mTitlePos,mBean.get(mTitlePos).getId(),1,1000);
}
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if(event.getAction() == KeyEvent.ACTION_DOWN){
if(mTitlePos < mBean.size()-1){
mTitlePos ++;
mListview_left.setSelection(mTitlePos);
requseFoodDetailedMenu(mTitlePos,mBean.get(mTitlePos).getId(),1,1000);
}
}
break;
}

return false;
}
});

}

/**
* 请求右侧详细数据
*/
private void requseFoodDetailedMenu(final int pos,int id,int pum,int size){
UIDataller.getDataller().gotoRequseFoodDetailedMenu(id, pum, size, new IBeanOnSuccessCB<ArrayList<FoodDetailsBean.ListBean>>() {
@Override
public void onSuccess(ArrayList<FoodDetailsBean.ListBean> bean) {
if(pos != mTitlePos){
return;
}
mFoodDetailsBean.clear();
mFoodDetailsBean = bean;
initRightAdapter();
}
});

}

/**
* 初始化右侧数据
*/
private void initRightAdapter() {
if(mAdapterRight == null){
GridLayoutManager linearLayoutManager = new GridLayoutManager(getActivity(),4);
mListViewRight.setLayoutManager(linearLayoutManager);
LogUtil.i("TAG","mListViewRight:"+mListViewRight.getWidth());
mAdapterRight = new FoodAdapterRight(getActivity(), mFoodDetailsBean, mListViewRight);
mAdapterRight.setOnKey(this);
mListViewRight.setAdapter(mAdapterRight);
}else{
mAdapterRight.setDataList(mFoodDetailsBean);
mListViewRight.setAdapter(mAdapterRight);
}

}

/**
* 请求左侧分类列表
*/
private void requseFoodType() {
UIDataller.getDataller().gotoRequseFoodType(new IBeanOnSuccessCB<FoodBean>() {
@Override
public void onSuccess(FoodBean bean) {
mWelcomeTextView.setText(bean.getClassName());
if(bean.getList() != null && bean.getList().size()>0){
mBean = bean.getList();
initTitleType();
requseFoodDetailedMenu(0,mBean.get(0).getId(),1,1000);
}
}
});
}

/**
* 初始化左边分类并设置值
*/
private void initTitleType() {

FoodAdapter adapter = new FoodAdapter(getActivity(),mBean);
mListview_left.setAdapter(adapter);
}

@Override
public void onItemClick(View view, int position) {
BaseActivity.getInstance().showToast("单击:"+position,0);
}

@Override
public void onKey(int keykode, View view, int position) {
switch (keykode){
case KeyEvent.KEYCODE_DPAD_LEFT:
mListview_left.setFocusable(true);
mListview_left.requestFocus();
mListview_left.setSelection(mTitlePos);
break;
}
}
}
左侧listview的adapter
package com.hhzt.iptv.lvb_w8.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.hhzt.iptv.R;
import com.hhzt.iptv.lvb_w8.bean.FoodBean;
import com.hhzt.iptv.lvb_y.utils.StringUtil;

import java.util.ArrayList;

/**
* Created by Administrator on 2017-12-09.
*/

public class FoodAdapter extends BaseAdapter {

private static final String TAG = FoodAdapter.class.getSimpleName();
private Context mContext;
private ArrayList<FoodBean.ListBean> mDataList = new ArrayList<>();
private int mSelection = 0;

public FoodAdapter(Context mContext, ArrayList<FoodBean.ListBean> mDataList) {
this.mContext = mContext;
this.mDataList = mDataList;
}

@Override
public int getCount() {
if (null != mDataList)
return mDataList.size();
return 0;
}

@Override
public Object getItem(int i) {
if (null != mDataList)
return mDataList.get(i);
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (null == view){
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.item_left, null);
viewHolder.textContent = (Button) view.findViewById(R.id.text_content);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
if (null != viewHolder.textContent && null != mDataList && mDataList.size()>0){
viewHolder.textContent.setText(StringUtil.setText(mDataList.get(position).getName()));

}else {
Log.i(TAG, "getView: null == mDataList");
}
return view;
}

public int getSelection() {
return mSelection;
}

public void setSelection(int selection) {
mSelection = selection;
notifyDataSetChanged();
}

class ViewHolder{
Button textContent;
}
}
adapter里面的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/text_content"
android:layout_width="match_parent"
android:layout_height="@dimen/layx60"
android:textColor="@color/bottom_city_color"
android:gravity="center"
android:layout_gravity="center"
android:background="@null"
android:textSize="@dimen/layx24"/>
</LinearLayout>
RecyclerView的adapter适配器
package com.hhzt.iptv.lvb_w8.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hhzt.iptv.R;
import com.hhzt.iptv.lvb_w8.bean.FoodDetailsBean;

import java.util.ArrayList;

/**
* Created by zyf on 2017/5/8.
* 右边RecyclerView适配器
*/

public class FoodAdapterRight extends RecyclerView.Adapter<FoodAdapterRight.ViewHolder> {

private static final String TAG = FoodAdapterRight.class.getSimpleName();
private final LayoutInflater mInflater;
private final int w;
private final int h;
private Context mContext;
private ArrayList<FoodDetailsBean.ListBean> mDataList = new ArrayList<>();
private RecyclerView mRecyclerView;
private OnItemClickListener listener;

public FoodAdapterRight(Context context, ArrayList<FoodDetailsBean.ListBean> dataList , RecyclerView recyclerView) {
mContext = context;
mDataList = dataList;
mRecyclerView = recyclerView;
w = mRecyclerView.getWidth();
h = mRecyclerView.getHeight();
mInflater = LayoutInflater.from(context);
}

@Override
public FoodAdapterRight.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_right,parent,false);

return new ViewHolder(view);
}

@Override
public void onBindViewHolder(FoodAdapterRight.ViewHolder holder, final int position) {
if (null != mDataList && mDataList.size() > 0) {
ViewGroup.LayoutParams parms = holder.itemView.getLayoutParams();
parms.width = w/4-20;
parms.height = h/2-20;
holder.itemView.setLayoutParams(parms);
holder.item_name.setText(mDataList.get(position).getName());
holder.item_price.setText("¥"+mDataList.get(position).getPrice());
Glide.with(mContext).load(mDataList.get(position).getPictureUrl()).into(holder.item_image);
holder.item_btn.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_LEFT:
if(event.getAction() ==KeyEvent.ACTION_DOWN){
if(position%4==0){
listener.onKey(KeyEvent.KEYCODE_DPAD_LEFT,v,position);
return true;
}
}
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
if(event.getAction() ==KeyEvent.ACTION_DOWN){
listener.onItemClick(v,position);
return true;
}
break;
}
return false;
}
});
} else {
Log.i(TAG, "getView: null == mDataList");
}
}

@Override
public int getItemCount() {
return mDataList.size();
}


class ViewHolder extends RecyclerView.ViewHolder {
Button item_btn;
ImageView item_image;
TextView item_name;
TextView item_price;
TextView select_share;
ViewHolder(View itemView) {
super(itemView);
item_btn = (Button) itemView.findViewById(R.id.item_btn);
item_image = (ImageView) itemView.findViewById(R.id.item_image);
item_name = (TextView) itemView.findViewById(R.id.item_name);
item_price = (TextView) itemView.findViewById(R.id.item_price);
select_share = (TextView) itemView.findViewById(R.id.select_share);
}
}

public void setDataList( ArrayList<FoodDetailsBean.ListBean> dataList){
mDataList.clear();
mDataList = dataList;
notifyDataSetChanged();
}
public interface OnItemClickListener {
void onItemClick(View view , int position);
void onKey(int keykode,View view , int position);
}

public void setOnKey(OnItemClickListener listener){
this.listener = listener;
}

}
适配器里面的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item__view_container"
android:layout_width="wrap_content"
android:descendantFocusability="beforeDescendants"
android:layout_height="wrap_content">

<Button
android:id="@+id/item_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/vod_item_bg_selector" />



<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/layx7"
android:orientation="vertical"
>
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="fitXY" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/white"
android:paddingLeft="@dimen/layx20"
android:paddingRight="@dimen/layx10"
android:layout_weight="1">
<TextView
android:id="@+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:ellipsize="marquee"
android:gravity="center_vertical|left"
android:paddingBottom="@dimen/layx5"
android:paddingTop="@dimen/layx5"
android:singleLine="true"
android:textColor="@color/red"
android:textSize="@dimen/layx20" />

<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/item_price"
android:ellipsize="marquee"
android:gravity="center_vertical|left"
android:paddingBottom="@dimen/layx5"
android:paddingTop="@dimen/layx5"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="@dimen/layx20" />


<TextView
android:id="@+id/select_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="bottom|center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingBottom="@dimen/layx5"
android:paddingTop="@dimen/layx5"
android:singleLine="true"
android:textColor="@color/red"
android:textSize="@dimen/layx20" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>

上述里面的button的背景选择器都没有上传,因为带有图片
 
 



Android为TV端助力 listview与recyclerview上下联动

标签:dad   lock   private   you   extc   abi   this   schema   coding   

原文地址:http://www.cnblogs.com/xiaoxiaing/p/8032046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!