我有一个与RecyclerView相关的查询。我希望获得点击的项目,并在同一布局中将其设置为textview,然后将值设置为textview,更新适配器。
这是我的recyclerview.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">
<RelativeLayout
android:id="@+id/rl_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageView_flag"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/usa" />
<TextView
android:id="@+id/textview_currency_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 USD(United States of America)"
android:layout_centerVertical="true"
android:textColor="#000"
android:layout_toEndOf="@+id/imageView_flag"
android:layout_marginStart="14dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_exchange_rate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rl_container"/>
</RelativeLayout>
这是我的片段类:
public class ExchangeRatesFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private OnFragmentInteractionListener mListener;
private String mParam1;
private String mParam2;
RetrofitClient retrofitClient;
RestInterface service;
ArrayList<ExchangeRate> exchangeRatesArraylist;
private RecyclerView mRecyclerView;
private ExchangeRateAdapter exchangeRateAdapter;
TextView textview_currency_info;
ImageView imageView_flag;
public ExchangeRatesFragment() {}
public static ExchangeRatesFragment newInstance(String param1, String param2) {
ExchangeRatesFragment fragment = new ExchangeRatesFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_exchange_rates, container, false);
exchangeRatesArraylist = new ArrayList<>();
retrofitClient = new RetrofitClient();
service = retrofitClient.getAPIClient(WebServiceUrls.DOMAIN_MAIN);
textview_currency_info = (TextView) rootView.findViewById(R.id.textview_currency_info);
imageView_flag = (ImageView) rootView.findViewById(R.id.imageView_flag);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_exchange_rate);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
get_exchange_rate("USD");
return rootView;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
public void get_exchange_rate(String from){
service.exchange_rate(from, new Callback<JsonElement>() {
@Override
public void success(JsonElement jsonElement, Response response) {
//this method call if webservice success
try {
JSONObject jsonObject = new JSONObject(jsonElement.toString());
final JSONArray exchange_rate = jsonObject.getJSONArray("exchange_rate");
for(int i=0; i<exchange_rate.length(); i++){
JSONObject currencyNews = exchange_rate.getJSONObject(i);
String short_name = currencyNews.getString("short_name");
String full_name = currencyNews.getString("full_name");
String flag = currencyNews.getString("flag");
String chang_value =currencyNews.getString("chang_value");
ExchangeRate currencyConverter = new ExchangeRate(short_name, full_name, flag, chang_value);
exchangeRatesArraylist.add(currencyConverter);
}
exchangeRateAdapter = new ExchangeRateAdapter(getContext(), exchangeRatesArraylist);
mRecyclerView.setAdapter(exchangeRateAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getActivity(),"Please check your internet connection", Toast.LENGTH_LONG ).show();
}
});
}
这是我的适配器类:
public class ExchangeRateAdapter extends RecyclerView.Adapter<ExchangeRateAdapter.ViewHolder>{
private ArrayList<ExchangeRate> mArrayList;
private Context context;
private final LayoutInflater mInflater;
public ExchangeRateAdapter(Context context, ArrayList<ExchangeRate> arrayList) {
this.mInflater = LayoutInflater.from(context);
mArrayList = arrayList;
context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.exchange_rate_items, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int i) {
holder.relativeLayout.setOnClickListener(clickListener);
holder.textView_full_name.setTag(holder);
holder.textView_short_name.setText(mArrayList.get(i).getShort_name());
holder.textView_full_name.setText(mArrayList.get(i).getFull_name());
holder.textview_currency_value.setText(mArrayList.get(i).getChang_value());
Picasso.with(context).load("http://uploads/country_flag/"+ mArrayList.get(i).getFlag()).into(holder.imageView_flag);
}
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
ViewHolder holder = (ViewHolder) view.getTag();
int position = holder.getPosition();
ExchangeRate person = mArrayList.get(position);
String businessids = person.getFull_name();
Intent intent = new Intent(context, test.class);
intent.putExtra("businessids", businessids);
context.startActivity(intent);
}
};
@Override
public int getItemCount() {
return mArrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView textView_short_name, textView_full_name, textview_currency_value;
private ImageView imageView_flag;
RelativeLayout relativeLayout;
ViewHolder(View itemView) {
super(itemView);
textView_short_name = (TextView)itemView.findViewById(R.id.textView_short_name);
textView_full_name = (TextView)itemView.findViewById(R.id.textView_full_name);
imageView_flag = (ImageView) itemView.findViewById(R.id.imageView_flag);
textview_currency_value = (TextView)itemView.findViewById(R.id.textview_currency_value);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.rl);
}
}
}
我尝试了很多在SO上发布的选项,但我没有任何解决方案。如何解决这个问题?当我将onclicklistener设置为ViewHolder
时,我在每个项目clicklistener上获取的位置值仅为-1。
答案 0 :(得分:0)
一些事情:
当您从单击的视图中获取标记时,您正在"holder.textView_full_name.setTag(holder)"
上设置标记(这将是相对布局,因为这是您设置点击监听器的地方(holder.relativeLayout.setOnClickListener(clickListener);)
无论如何这都是必要的。
在您的点击监听器中,调用'getAdapterPosition()'
,这应该返回您期望的内容。
修改:Codepath对RecyclerView
有一个很好的概述,有点过时,但仍然相关。
答案 1 :(得分:0)
您可以使用此依赖项
implementation 'io.reactivex:rxjava:1.2.1'
implementation 'io.reactivex:rxandroid:1.2.1'
仅举例: 参考 与此一样,您可以单击RecyclerView
adapter.busNoItemClick().doOnNext(this::getBusNum).subscribe();
private void getBusNum(String busNo){
((TextView)findViewById(R.id.txtBusNumberStudent)).setText(busNo);
}
在适配器中你必须写这个:
@Override
public void onBindViewHolder(AdapterBusRouteParent.ViewHolder holder, int position) {
holder.busNumber.setOnClickListener(view -> publishSubject.onNext(busRouteParents.get(position)));
}
public Observable<String>busNoItemClick(){
return publishSubject.observeOn(AndroidSchedulers.mainThread());
}
希望这对你有所帮助。
答案 2 :(得分:0)
一种方法是拥有
int SELECTED_INDEX = 0;
public void setSelectedItemIndex(int position){
SELECTED_INDEX = position;
/*if(adapterCallback!=null) { //If you have a callback interface
adapterCallback.onItemClick(position);
}*/
notifyDataSetChanged();
}
持有人类别:
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
//Item Declarations
public MyViewHolder(View itemView) {
super(itemView);
//View Binding
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v){
if(v==itemView){
setSelectedItemIndex(getAdapterPosition());
}
}
}
在onBindViewHolder方法中:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if(position==SELECTED_INDEX){
//Implement your steps
}
else{
//Default Value
}
}
希望它有所帮助!