1.
package com.multak.cookaraclient.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.multak.cookaraclient.R; import com.multak.cookaraclient.info.MusicInfo; import java.util.List; /** * Created by guoxw on 2017/6/19. */ public class ProRecycleViewAdapter extends RecyclerView.Adapter<ProRecycleViewAdapter.ViewHolder> implements View.OnClickListener { private static final String TAG = "ProRecycleViewAdapter"; /** * */ private ProRecycleViewAdapter.OnItemClickListener mOnItemClickListener=null; List<MusicInfo> data; Context context; int type; /** * 点击的条目 */ int num=-2; public void textSize(int type){ this.type=type; } public ProRecycleViewAdapter(List<MusicInfo> data, Context context) { this.data = data; this.context = context; } @Override public ProRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(context).inflate(R.layout.prorecycleview_item_layout,parent,false); final ProRecycleViewAdapter.ViewHolder holder=new ProRecycleViewAdapter.ViewHolder(view); /** * */ view.setOnClickListener(this); return holder; } @Override public void onBindViewHolder(ProRecycleViewAdapter.ViewHolder holder, int position) { MusicInfo musicInfo=data.get(position); holder.textView_SongName.setText(musicInfo.getMusicName()); holder.textView_SongSinger.setText(musicInfo.getMusicSinger()); int x=position+1; holder.textView_no.setText(""+x); if(type==2){ holder.textView_SongName.setTextColor(context.getResources().getColor(R.color.colorHomeText)); holder.textView_SongSinger.setTextColor(context.getResources().getColor(R.color.colorHomeText)); }else if(type==1){ holder.textView_SongName.setTextColor(context.getResources().getColor(R.color.colorSingerName)); holder.textView_SongSinger.setTextColor(context.getResources().getColor(R.color.colorSingerName)); } if(num==position){ Log.d(TAG, "onBindViewHolder: NUM: "+num); Log.d(TAG, "onBindViewHolder: Position: "+position); holder.textView_SongName.setTextColor(context.getResources().getColor(R.color.colorAccent)); }
else {
//恢复
}
/** * */ holder.itemView.setTag(position); } @Override public int getItemCount() { return data.size(); } class ViewHolder extends RecyclerView.ViewHolder{ View viewItem; TextView textView_no; TextView textView_SongName,textView_SongSinger; public ViewHolder(View itemView) { super(itemView); viewItem=itemView; textView_SongName= (TextView) itemView.findViewById(R.id.textView_SongName); textView_SongSinger= (TextView) itemView.findViewById(R.id.textView_SongSinger); textView_no= (TextView) itemView.findViewById(R.id.textView_no); } } /** * * @param */ public static interface OnItemClickListener{ void onItemClick(View view,int position); } /** * * @param v */ @Override public void onClick(View v) { if(mOnItemClickListener != null) { mOnItemClickListener.onItemClick(v,(int)v.getTag()); } } /** * * @param listener */ public void setOnItemClickListener(ProRecycleViewAdapter.OnItemClickListener listener) { this.mOnItemClickListener=listener; } public void setItemClickColor(int num){ this.num=num; } }
2.
package com.multak.cookaraclient.fragment;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.multak.cookaraclient.R;
import com.multak.cookaraclient.adapter.ProRecycleViewAdapter;
import com.multak.cookaraclient.info.MusicInfo;
import com.multak.cookaraclient.info.ToastUtils;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class ProFragment extends Fragment {
private static final String TAG = "ProFragment";
View view;
RecyclerView recyclerView;
ProRecycleViewAdapter proRecycleAdapter;
List<MusicInfo>data=new ArrayList<>();
public ProFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view=inflater.inflate(R.layout.fragment_pro, container, false);
recyclerView= (RecyclerView) view.findViewById(R.id.recycleView_Pro);
recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
final Context context=getActivity();
addMusicInfo();
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
proRecycleAdapter=new ProRecycleViewAdapter(data,context);
recyclerView.setAdapter(proRecycleAdapter);
proRecycleAdapter.setOnItemClickListener(new ProRecycleViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
ToastUtils.showtoast(getActivity(),""+position);
Log.d(TAG, "onBindViewHolder: TOAST: "+position);
proRecycleAdapter.setItemClickColor(position);
proRecycleAdapter.notifyDataSetChanged();
}
});
return view;
}
public void addMusicInfo(){
data.add(new MusicInfo("我知道","阎韦伶"));
data.add(new MusicInfo("千里之外","周杰伦"));
data.add(new MusicInfo("同桌的你","老狼"));
data.add(new MusicInfo("我知道","阎韦伶"));
data.add(new MusicInfo("千里之外","周杰伦"));
data.add(new MusicInfo("同桌的你","老狼"));
data.add(new MusicInfo("我知道","阎韦伶"));
data.add(new MusicInfo("千里之外","周杰伦"));
data.add(new MusicInfo("同桌的你","老狼"));
data.add(new MusicInfo("我知道","阎韦伶"));
data.add(new MusicInfo("千里之外","周杰伦"));
data.add(new MusicInfo("同桌的你","老狼"));
data.add(new MusicInfo("我知道","阎韦伶"));
data.add(new MusicInfo("千里之外","周杰伦"));
data.add(new MusicInfo("同桌的你","老狼"));
data.add(new MusicInfo("我知道","阎韦伶"));
data.add(new MusicInfo("千里之外","周杰伦"));
data.add(new MusicInfo("同桌的你","老狼"));
}
public void selectOperate(final int posi,View view){
View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_song, null);
final PopupWindow popupWindow = new PopupWindow(contentView,600, LinearLayout.LayoutParams.WRAP_CONTENT, true);
WindowManager manager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
TextView textView_xiai= (TextView) contentView.findViewById(R.id.textView_xiai);
TextView textView_yuyue= (TextView) contentView.findViewById(R.id.textView_yuyue);
TextView textView_zhiding= (TextView) contentView.findViewById(R.id.textView_zhiding);
TextView textView_bofang= (TextView) contentView.findViewById(R.id.textView_bofang);
textView_xiai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showtoast(getActivity(),"喜爱操作");
popupWindow.dismiss();
}
});
textView_yuyue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showtoast(getActivity(),"预约操作");
popupWindow.dismiss();
}
});
textView_zhiding.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showtoast(getActivity(),"置顶操作");
popupWindow.dismiss();
}
});
textView_bofang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showtoast(getActivity(),"播放操作");
popupWindow.dismiss();
}
});
int popupWidth = view.getMeasuredWidth();
int popupHeight = view.getMeasuredHeight();
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
Log.d(TAG, "selectOperate: 坐标:"+x+"&&"+y);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(view, Gravity.TOP|Gravity.LEFT, x+200, y+140);
popupWindow.showAsDropDown(view);
view.setAlpha(1f);
}
}
3.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:layout_width="50dp" android:layout_height="50dp" android:textSize="@dimen/songnumbersize" android:gravity="center" android:textColor="@color/colorSingerName" android:id="@+id/textView_no"/> <LinearLayout android:layout_marginLeft="@dimen/imagemagintext" android:layout_height="50dp" android:layout_width="0dp" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/textView_SongName" android:layout_width="match_parent" android:layout_height="30dp" android:paddingTop="5dp" android:gravity="center_vertical" android:textColor="@color/colorSongName" android:textSize="@dimen/songnamesize"/> <TextView android:id="@+id/textView_SongSinger" android:layout_width="match_parent" android:layout_height="20dp" android:gravity="center_vertical" android:textColor="@color/colorSongName" android:textSize="@dimen/songsingersize"/> </LinearLayout> </LinearLayout>
<FrameLayout 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" tools:context="com.multak.cookaraclient.fragment.ProFragment"> <!-- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/recycleView_Pro" android:textSize="23sp" android:text="this is a ProFragment" /> </FrameLayout>
错误在于:在if()加入else{} 对item未选中做恢复操作。