Access to the first element of a list when list view is loaded in android

If there is no event like list.onLoadListener (the list is executed with filling in its data), how can I access the first line of the list?

As the list repeats its line for performance, this listener does not exist, which is understandable. But I need to access the first element of the list if at least one element (first element, position 0).

After installing the adapter in the list

list.getChildAt(0) 

returns null. So do I need to set a delay to access the first item?

We can access the list items (views in this item) by using the list click listener. I want to use an element when I can be sure that the first element of the list is full.

I am playing a video in a list item using a TextureView. Therefore, as soon as the list is filled with its element, I want to automatically play the video with the first item (without any click or user interaction).

Here is my code: -

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); list = (ListView) v.findViewById(R.id.list); videoListDatas = new ArrayList<VideoListData>(); adapter = new MyVideoListAdapterNew(getActivity(), videoListDatas); list.setAdapter(adapter); getVideoList(); //Method which get data from server } 

Here is the implementation of the getVideoList () method

 private void getVideoList() { final MyProgressDialog progressDialog = new MyProgressDialog(context); new Thread(new Runnable() { @Override public void run() { try { // Implementation goes here fill array with data } catch (Exception e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); progressDialog.dismiss(); } }); } }).start(); } 

And here is my adapter

  public class MyVideoListAdapterNew extends BaseAdapter { Context context; private LayoutInflater inflater; ArrayList<VideoListData> videoListDatas; public MyVideoListAdapterNew(FragmentActivity fragmentActivity, ArrayList<VideoListData> videoListDatas) { context = fragmentActivity; this.videoListDatas = videoListDatas; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return videoListDatas.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return videoListDatas.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.myvideo_row, null); holder = new ViewHolder(); holder.flVideo = (FrameLayout) convertView .findViewById(R.id.flVideo); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } try { //Filling views by getting values from array } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(position == 0 ){ //Code to play first video automatically }else{ } return convertView; } private static class ViewHolder { FrameLayout flVideo; } } 

I know that the code will not help, but I just send it to the suggestion of some people.

Thanks.

0
source share
4 answers

I found a solution for this situation from this thread: - fooobar.com/questions/120926 / ...

My requirement was to access the first element of the list as soon as the list finished loading the data. This list of paths waits until its data is updated / loaded.

Thanks.

0
source

You can use the Custom Adapter to present the list, and then you can intercept the view at any specific position in the public View getView(int position, View convertView, ViewGroup parent) . Here you can check that

 if(position==0){ //do your stuff } 

See this answer for custom adapter

0
source

When do you fill out the list? When do you want to access the first item? If you use fragments, you have the onViewCreated, onCreateView methods, and you can populate the list in the onCreateView method and access it in the onViewCreated methods. With actions different, you can populate the list in onCreate () and later onStart () or onResume () access to the first element.

0
source

I don't know if this is the best approach, but you can try.

Create a listener that looks something like this:

 public interface ViewLoadedInterface { public void onFirstViewLoaded(/**add params if you like*/); } 

Pass Interface to your Adapter . You only call the onFirstViewLoaded() method when your first View returns, and then never calls it again for this instance, otherwise you will have problems starting the recycling.

When it is called, you then execute list.getChildAt(0) in the interface implementation.

So, in the end, the implementation will look something like this:

 public class YourActivityClass extends Activity implements ViewLoadedInterface { ... @Override public void onFirstViewLoaded() { ... something = list.getChildAt(0); ... } } 
0
source

Source: https://habr.com/ru/post/927612/


All Articles