несколько повторных просмотров за одно мероприятие - PullRequest
0 голосов
/ 11 ноября 2019

Я пытаюсь создать приложение, которое имеет так много повторений, как и просмотр карт, и все они находятся в одном действии. Итак, списков много, и эти списки имеют горизонтальную ориентацию. Я выбираю данные из файла JSON и показываю их в этих списках. Я знаю, что для этого есть долгий путь. Но я верю, что есть и более эффективный способ сделать это. Я не эксперт. Но я не хочу писать так много разных обзоров, макетов и адаптеров. Так можете ли вы научить меня, как это сделать?

Вот мои коды;

Games.class;

public class Games extends AppCompatActivity {

    RecyclerView recyclerView, recyclerView2;
    ArrayList<ModelGames> modelGames;
    private RequestQueue mQueue;
    GamesAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_games);

        modelGames = new ArrayList<>();

        recyclerView = findViewById(R.id.gamesPageRecyclerView);
        recyclerView2 = findViewById(R.id.gamesPageRecyclerView2);

        mQueue = Volley.newRequestQueue(this);

        releasedOn19();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        RecyclerView.LayoutManager gamesLiLayoutManager = linearLayoutManager;
        recyclerView.setLayoutManager(gamesLiLayoutManager);

        adapter = new GamesAdapter(this, modelGames);
        recyclerView.setAdapter(adapter);



    }

    private void releasedOn19() {
        String popularIn2019 = "https://api.rawg.io/api/games?dates=2019-01-01,2019-12-31&ordering=-added";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, popularIn2019, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("results");
                            for (int i = 0; i < jsonArray.length(); i++){
                                JSONObject result = jsonArray.getJSONObject(i);
                                String gameName = result.getString("name");
                                String gameImage = result.getString("background_image");
                                modelGames.add(new ModelGames(gameName, gameImage));
                            }
                            adapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        mQueue.add(request);
    }

}

ModelGames;

public class ModelGames {

    private String text, image;

    public ModelGames(String text, String image) {
        this.text = text;
        this.image = image;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

GamesAdapter;

public class GamesAdapter extends RecyclerView.Adapter<GamesAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<ModelGames> mList;

    GamesAdapter(Context context, ArrayList<ModelGames> list){
        mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.rv_game_items, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ModelGames getItem = mList.get(position);
        TextView gameName = holder.game_title;
        ImageView gameImage = holder.game_image;
        Glide.with(mContext).asBitmap().load(getItem.getImage()).into(gameImage);

        gameName.setText(mList.get(position).getText());

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView game_image;
        TextView game_title;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            game_image = itemView.findViewById(R.id.cardviewImage);
            game_title = itemView.findViewById(R.id.cardviewText);


        }
    }
}

А вот и мой Activity_games.xml. Я добавил второй просмотрщик. Но я не использовал это нигде

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

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:text="2019 Yılında Çıkan Oyunlar"
                android:textStyle="bold"
                android:textSize="18sp"
                android:gravity="center_horizontal|center_vertical"
                />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/gamesPageRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:text="2018 Yılında Çıkan Oyunlar"
                android:textStyle="bold"
                android:textSize="18sp"
                android:gravity="center_horizontal|center_vertical"
                />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/gamesPageRecyclerView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...