在android中滚动回收项目位置更改

时间:2016-06-20 05:21:55

标签: android android-recyclerview recycler-adapter

我在nestedview中创建了Recycle视图。嵌套项目垂直滚动,循环列表水平滚动。如果我滚动循环视图,循环项目的位置会发生变化。是否有人提供解决方案来解决此问题?

  try {
        JSONObject jsonRootObject = new JSONObject(data);
        JSONArray jsonArray = jsonRootObject.optJSONArray("new_hotel");

        //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("hotel_name").toString());
            movie.sethorizontal1_image(jsonObject.optString("logo").toString());
            movie.sethorizontal1_hotelid(jsonObject.optString("hotel_id").toString());
            movie.sethorizontal1_city(jsonObject.optString("city").toString());
            movieList.add(movie);

            //listview1.setHorizontalScrollBarEnabled(true);
            LinearLayoutManager horizontalLayoutManagaer
                    = new LinearLayoutManager(Home.this, LinearLayoutManager.HORIZONTAL, false);
            listview1.setLayoutManager(horizontalLayoutManagaer);
            listview1.setAdapter(adapter1);
            adapter1.notifyDataSetChanged();

        }

这是我的适配器代码:

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {

    private final Context mcontext;
    private final List<Movie> movieItems;
    TextView horz_title,amount;
    ImageView horz_image;

    public class MyViewHolder extends RecyclerView.ViewHolder {



        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(horz_image);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        if (m.gethorizontal1_title()!=null)
        {
            if (!m.gethorizontal1_title().equals("null"))
            {
                horz_title.setText(m.gethorizontal1_title());
            }}

    }

    @Override
    public int getItemCount() {
        return movieItems.size();
    }
}

这是我的xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />
    <android.support.design.widget.CoordinatorLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:background="?android:attr/colorBackground">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/materialup_appbar"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@null"


            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            >
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapse_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"


                app:expandedTitleTextAppearance="@android:color/transparent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                >​

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:src="@drawable/sample"
                    android:scaleType="fitXY"
                    android:id="@+id/homeimage"
                    android:adjustViewBounds="true"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="1.0"/>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/nest_scrollview"
            android:fillViewport="true"
            android:scrollbarStyle="insideOverlay"
            android:layout_gravity="fill_vertical">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hi UserName"
                    android:textSize="30dp"
                    android:gravity="left"
                    android:id="@+id/home_username"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:textSize="30dp"
                    android:gravity="left"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    />
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="10dp"
                    android:background="@drawable/horizontalline"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Discover New"
                    android:textSize="15dp"
                    android:gravity="left"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    />



                <android.support.v7.widget.RecyclerView
                    android:id="@+id/horizontal_recycler_view"
                    android:background="#fff"
                    android:layout_weight="1"
                    android:layout_width="match_parent"

                    android:layout_height="0dp"/>
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="10dp"
                    android:background="@drawable/horizontalline"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"/>


            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer" />

2 个答案:

答案 0 :(得分:1)

我没有测试过但它应该可以工作。每个MyViewHolder都拥有TextViewImageView。但是,您要将所有这些内容分配给HorizontalAdapter。这使得最新版本始终覆盖该值。您应该将TextViewImageView放在MyViewHolder

public static class MyViewHolder extends RecyclerView.ViewHolder {
    private TextView horz_title; 
    private 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);
    }
}

onBindViewHolder中,您应该像以下代码一样访问该视图。

holder.horz_title.setText(m.gethorizontal1_title());
Glide.with(mcontext).load(String.valueOf(url)).into(holder.horz_image);

答案 1 :(得分:0)

更新您的代码添加两种情况if和else总是滚动值更新

 if (m.gethorizontal1_title()!=null){
      if (!m.gethorizontal1_title().equals("null")){
            horz_title.setText(m.gethorizontal1_title());
         }else{
            horz_title.setText("");
         }
      }else {
         horz_title.setText("");
     }