Java 如何在listView中创建两个文本块,第一个在左对齐,第二个在右对齐?
如何在listView中设置两个文本块,第一个在左侧,另一个在右侧?我试图用两个文本视图创建一个新布局。但我不知道如何将TextView与listView连接,以及如何在TextView上设置文本。有人能帮我吗 我想要一份这样的清单Java 如何在listView中创建两个文本块,第一个在左对齐,第二个在右对齐?,java,android,android-layout,Java,Android,Android Layout,如何在listView中设置两个文本块,第一个在左侧,另一个在右侧?我试图用两个文本视图创建一个新布局。但我不知道如何将TextView与listView连接,以及如何在TextView上设置文本。有人能帮我吗 我想要一份这样的清单 假设你已经有了这个列表,并且你的2个文本视图的布局已经准备好了,只需使用这个适配器并将这个适配器设置为活动的recycler视图。如果您遇到任何问题,请告诉我 public class CountryCodeAdapter extends RecyclerView
假设你已经有了这个列表,并且你的2个文本视图的布局已经准备好了,只需使用这个适配器并将这个适配器设置为活动的recycler视图。如果您遇到任何问题,请告诉我
public class CountryCodeAdapter extends RecyclerView.Adapter<CountryCodeAdapter.ViewHolder> {
private CountryCodeActivity activity;
ArrayList<CountryCodeModel> list = new ArrayList<>();
int selected_pos = 0;
public CountryCodeAdapter(CountryCodeActivity activity, ArrayList<CountryCodeModel> list) {
this.activity = activity;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country_listing, parent, false);
return new ViewHolder(rootView);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.tv_row_CountryCodeActivity_countrycode.setText(list.get(holder.getAdapterPosition()).getDial());
holder.tv_row_CountryCodeActivity_countryname.setText(list.get(holder.getAdapterPosition()).getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("country_code", list.get(holder.getAdapterPosition()).getDial());
activity.setResult(activity.RESULT_OK, intent);
activity.finish();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tv_row_CountryCodeActivity_countryname,
tv_row_CountryCodeActivity_countrycode;
public ViewHolder(View itemView) {
super(itemView);
tv_row_CountryCodeActivity_countryname = itemView.findViewById(R.id.tv_row_CountryCodeActivity_countryname);
tv_row_CountryCodeActivity_countrycode = itemView.findViewById(R.id.tv_row_CountryCodeActivity_countrycode);
tv_row_CountryCodeActivity_countryname.setTypeface(AppClass.Lato_Regular);
tv_row_CountryCodeActivity_countrycode.setTypeface(AppClass.Lato_Regular);
}
}
}
1您应该使用RecyclerView而不是listView,对于RecyclerView,您可以使用item decorator实现所需的功能。 2但是,如果必须使用ListView(这种情况不太可能),可以通过检查列表项位置并将相应的边距设置为不推荐的布局来实现。
3还有另一种方法可以实现这一点,即使用不同的布局资源xml文件,但我不会使用最后两种变体。我更喜欢第一个。可能重复使用RecycleService它是recycler视图适配器类的重写方法