0
我已經創建了回收視圖,並且所有列表項都以水平格式顯示。當我打開應用程序時,最後一項顯示在第一位。最初我想從左到右滾動列表項目。如果我打開應用程序,我已經從右側滾動列表,因爲最後一項顯示在第一個。如何在recycleview中首先顯示第一個項目。如何在回收視圖中顯示第一項android
try {
JSONObject jsonRootObject = new JSONObject(data);
JSONArray jsonArray = jsonRootObject.optJSONArray("new_room");
//Iterate the jsonArray and print the info of JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// int id = Integer.parseInt(jsonObject.optString("hotel_id").toString());
String name = jsonObject.optString("hotel_name").toString();
final String image = jsonObject.optString("logo").toString();
Log.d("response", "response -----" + image);
Movie movie = new Movie();
movie.sethorizontal1_title(jsonObject.optString("room_name").toString());
movie.sethorizontal1_image(jsonObject.optString("room_images").toString());
movie.sethorizontal1_roomid(jsonObject.optString("room_id").toString());
movie.sethorizontal1_city(jsonObject.optString("city").toString());
movieList.add(movie);
runOnUiThread(new Runnable() {
@Override
public void run() {
listview1.setAdapter(adapter1);
listview1.setHorizontalScrollBarEnabled(true);
LinearLayoutManager horizontalLayoutManagaer
= new LinearLayoutManager(Home.this, LinearLayoutManager.HORIZONTAL, false);
listview1.setLayoutManager(horizontalLayoutManagaer);
adapter1.notifyDataSetChanged();}});
}
} catch (Exception e) {
e.printStackTrace();
}
適配器
公共類HorizontalAdapter擴展RecyclerView.Adapter {
private final Context mcontext;
private final List<Movie> movieItems;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView horz_title,amount;
public ImageView horz_image;
public MyViewHolder(View view) {
super(view);
horz_title = (TextView) view.findViewById(R.id.horz_title);
horz_image = (ImageView) view.findViewById(R.id.horz_image);
}
}
public HorizontalAdapter(Context mcontext, List<Movie> movieItems) {
this.mcontext = mcontext;
this.movieItems = movieItems;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.horizontal_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Movie m = movieItems.get(position);
URL url = null;
String image=m.gethorizontal1_image();
try {
url = new URL(image);
Glide.with(mcontext).load(String.valueOf(url)).into(holder.horz_image);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (m.gethorizontal1_title()!=null){
if (!m.gethorizontal1_title().equals("null")){
holder.horz_title.setText(m.gethorizontal1_title());
}
}
}
@Override
public int getItemCount() {
return movieItems.size();
}
}
此外添加這些lines.try JSONObject jsonRootObject = new JSONObject(data); JSONArray jsonArray = jsonRootObject.optJSONArray(「new_room」); –
我已經試過你的代碼,但它仍然是一樣的,我有我的適配器代碼花花公子 – sri
請任何人幫我解決這個問題 – sri