我有一个RecyclerView,其中连续包含两个textViews。我要做的就是更改特定行的单个textView的可见性。
例如一个行包含两个textview A和B。我想通过动态使用代码从活动传递行位置来更改特定行的textview A的可见性。我已经在互联网上冲浪,但是没有找到正确的实现方式。
我的代码:
我的适配器类
package com.transit.adarsh.catchmypath;
import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
List<StationData> listItem;
private Context context;
public MyAdapter(List<StationData> listItem, Context context) {
this.listItem = listItem;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StationData stationData = listItem.get(position);
holder.platform.setText(stationData.getPlatform());
holder.station_name.setText(stationData.getEng_station());
Log.i("DATASSSQ","call");
}
@Override
public int getItemCount() {
return listItem.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView platform;
public TextView station_name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
platform = (TextView)itemView.findViewById(R.id.platText);
station_name = (TextView)itemView.findViewById(R.id.station_name);
}
}
}
StationData类:
package com.transit.adarsh.catchmypath;
public class StationData {
private String platform;
private String eng_station;
private String hin_station;
private double latitude;
private double longitude;
public StationData( String platform, String eng_station, String hin_station, double latitude, double longitude) {
this.platform = platform;
this.eng_station = eng_station;
this.hin_station = hin_station;
this.latitude = latitude;
this.longitude = longitude;
}
public String getPlatform() {
return platform;
}
public String getEng_station() {
return eng_station;
}
public String getHin_station() {
return hin_station;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
}
RecyclerView类:
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
stationDataList = new ArrayList<>();
stationDataList.add(new StationData("Kopar","PF 2","Kopar",
"कोपर",19.211237,73.077613));
stationDataList.add(new StationData("Diva","PF 2","Diva",
"दिवा",19.188439,73.042223));
stationDataList.add(new StationData("Mumbra","PF 2","Mumbra",
"मुंब्रा",19.190266,73.023015));
stationDataList.add(new StationData("Kalva","PF 2","Kalva",
"कलवा",19.196362,72.998234));
stationDataList.add(new StationData("Thane","PF 4","Thane",
"ठाणे",19.186472,72.97547));
adapter = new MyAdapter(stationDataList, this);
recyclerView.setAdapter(adapter);
我是编程世界的新手,如果有人帮助我,那就太好了。
答案 0 :(得分:1)
您应该将属性[ visibility ]存储在模型中,并据此做出决定。
<<<<<<< HEAD
...lots of code
line changed by me
...lots of code
line changed by me
...lots of code
=======
>>>>>>> origin/master
在此处处理可见性并更新视图。
@State var round = 0
@State var showingAlert:Bool {round > 10 ? true : false}
func result(player: Int, app: Int) {
if player > app {
round += 1
}
else {
round += 1
}
}
var body: some View {
Button(action: {self.result(player: 1, app: 1)}) {
Text("Button")
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("title"), message: Text("message"), dismissButton: .default(Text("Continue"))
)
}
从具有定位和可见度的活动中致电class StationData {
...
private int visibility = View.VISIBLE;
public int getVisibility() {
return visibility;
}
public void setVisibility(int visibility) {
this.visibility = visibility;
}
...
}
。
@Override
public void onBindViewHolder(@NonNull final SuggestionHomeAdapter.ViewHolder holder, final int position) {
final StationData stationData = listItem.get(position);
holder.station_name.setVisibility(stationData.getVisibility());
...
}