2016-08-16 64 views
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(); 
     } 

适配器

公共类Horizo​​ntalAdapter扩展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(); 
} 

}

回答

0
 //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(); 
    } 

请使用上面的代码,我也做了一些变化,上面的代码。 请让我知道如果不工作。 会发现新的解决方案,并请在这种情况下共享您的适配器的代码。

+0

此外添加这些lines.try JSONObject jsonRootObject = new JSONObject(data); JSONArray jsonArray = jsonRootObject.optJSONArray(“new_room”); –

+0

我已经试过你的代码,但它仍然是一样的,我有我的适配器代码花花公子 – sri

+0

请任何人帮我解决这个问题 – sri

相关问题