I add to linearLayout, (located inside scrollView) view'hi using layoutInflater:

for(int i = 0; i< data.length; i++){ inflatedView = layoutInflater.inflate(R.layout.owner_item, scrollinlayout, false); ... linearLayout.addView(inflatedView); } 

How can I get the id of the added view outside of this loop? More precisely, I want to have access to the entire list of added views, i.e. such as ArrayList<Integer> or ArrayList<View> , so that you can then call its methods for addedView.getBackground().setAlpha(128); for example: addedView.getBackground().setAlpha(128);

Thank!

  • one
    Announce your list in the field of visibility of the place where you are going to call it afterwards - Vitaly Shebanits
  • one
    In general, your question suggests that you need to use a RecyclerView + Adapter with storage. If you don't want to add a bunch of boilerplate, try a tick library like this github.com/vivchar/RendererRecyclerViewAdapter - Serge Markov

1 answer 1

If you do based on the conditions of the problem, you can do this:

 for(int i = 0; i< data.linearLayout.getChildCount()-1; i++){ linearLayout.getChildAt(i).yourMethod(); } 

Another option (again from the condition), as you suggested yourself, is to add all the View to ArrayList : myViews.add(inflatedView) and then also find it. This option is better if you have other non-task, View in linearLayout .

But the most correct, as you were told in the comments, is to use RecyclerView :

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter .ViewHolder> { private List<MyData> dataList; private Context ctx; //ΠΌΠΎΠΆΠ½ΠΎ ΡƒΠ±Ρ€Π°Ρ‚ΡŒ Ссли Ρ€Π°Π±ΠΎΡ‚Π° с контСкстом Π² Π°Π΄Π°ΠΏΡ‚Π΅Ρ€Π΅ Π½Π΅ Π½ΡƒΠΆΠ½Π° public MyAdapter (Context ctx, List data) { this.ctx = ctx; dataList = data; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.owner_item, viewGroup, false); return new MyAdapter.ViewHolder(v); } @Override public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) { //ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡ‚ΡŒ Π½ΡƒΠΆΠ½Ρ‹Π΅ дСйствия viewHolder.txt.setText(dataList.get(i).getName()); //Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ Ρ‚Π°ΠΊΠΈΠ΅ } @Override public int getItemCount() { return dataList.size(); } class ViewHolder extends RecyclerView.ViewHolder { private TextView txt; ViewHolder(View itemView) { super(itemView); txt = itemView.findViewById(R.id.myTV); txt.setOnClickListener(...); } } } 

Fill RecyclerView like this:

 recyclerView.setAdapter(new MyAdapter(this,myDataList)); recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
  • Yes thank you! I know about RecyclerView, just the task was to use ScrollView and nothing else - Stanly T