0

I'm adding a launcher with about 10 icons to my application and got it to work vertically but can't find a solid simple solution to rotate it so that it scrolls the 10 icons horizontally.

Java:

private PackageManager manager;
private List<AppDetail> apps;
private ListView list;

private void loadListView(){
    //list = (ListView)findViewById(R.id.apps_list);//was apps_list
    list = (ListView) findViewById(R.id.apps_list);//was apps_list

    ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this,
            R.layout.list_item,
            apps) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
            appIcon.setImageDrawable(apps.get(position).icon);

            TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
            appLabel.setText(apps.get(position).label);

            TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
            appName.setText(apps.get(position).name);

            return convertView;
        }
    };
    list.setAdapter(adapter); //Put the list on the screen
}

Inside the XML Layout

<ListView
    android:id="@+id/apps_list"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="50dp"
    android:layout_marginEnd="29dp"
    android:orientation="horizontal"/>

I saw something called a HorizontalScrollView but it doesn't seem to be a direct replacement and just crashes the app.

It looks like I'm supposed to use a RecyclerView and maybe something like this.

Java:

private void loadListView(){


    mRecyclerView = (RecyclerView) findViewById(R.id.apps_list);
    ArrayAdapter<AppDetail> mAdapter = new ArrayAdapter<AppDetail>(this,
            R.layout.list_item,
            apps) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
            appIcon.setImageDrawable(apps.get(position).icon);

            TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
            appLabel.setText(apps.get(position).label);

            TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
            appName.setText(apps.get(position).name);

            return convertView;
        }
    };
    mRecyclerView.setAdapter(mAdapter); //Put the list on the screen
}

XML:

<android.support.v7.widget.RecyclerView
    android:id="@+id/apps_list"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="50dp"
    android:layout_marginEnd="29dp"
    android:orientation="horizontal"
    android:scrollbars="horizontal"/>

but I'm getting the error:

Error:(1212, 34) error: incompatible types: ArrayAdapter cannot be converted to Adapter

Thanks for any guidance.

Mickey D
  • 347
  • 2
  • 12
  • 1
    you should use a recyler view, instead of a list view. You can set the layout to horizontal very easily – Levi Moreira Mar 26 '18 at 23:55
  • I tried it as a direct replacement but it crashes. I am not sure what else has to change in the code to have it work. Thanks – Mickey D Mar 26 '18 at 23:59
  • https://stackoverflow.com/questions/28460300/how-to-build-a-horizontal-listview-with-recyclerview This could help. – Eiji Mar 27 '18 at 00:17
  • I think my problem is just trying to put the launcher icon arrays into the RecyclerView adapter. The error I'm seeing when converting is Error:(1212, 34) error: incompatible types: ArrayAdapter cannot be converted to Adapter. – Mickey D Mar 27 '18 at 00:31

1 Answers1

2

I've implemented your listview into a recycler view, here's the adapter:

public class SpecialAdapter extends RecyclerView.Adapter<SpecialAdapter.SpecialViewHolder> {

    private ArrayList<AppDetail> items;
    private Context context;
    private OnClickItem clickListener;

    public SpecialAdapter(ArrayList<AppDetail> items, Context context) {
        this.items = items;
        this.context = context;
    }

    public void setClickListener(OnClickItem clickListener) {
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public SpecialViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new SpecialViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull SpecialViewHolder holder, int position) {
        holder.appIcon.setImageDrawable(items.get(position).icon);
        holder.appLabel.setText(items.get(position).label);
        holder.appName.setText(items.get(position).name);

    }

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

    class SpecialViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView appIcon;
        TextView appLabel;
        TextView appName;

        public SpecialViewHolder(View itemView) {
            super(itemView);
            appIcon = (ImageView) itemView.findViewById(R.id.item_app_icon);
            appLabel = (TextView) itemView.findViewById(R.id.item_app_label);
            appName = (TextView) itemView.findViewById(R.id.item_app_name);
             itemview.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            clickListener.onClickItem(getAdapterPosition());
        }
    }

    public interface OnClickItem {
        void onClickItem(int pos);
    }
}

To use it, just replace your ListView to a RecyclerVie in your xml and use this:

public void setUpRecyclerView() {
    RecyclerView list = findViewById(R.id.apps_list);

    list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    SpecialAdapter adapter = new SpecialAdapter(apps,this);
    adapter.setClickListener(this);
    list.setAdapter(adapter);

}

Also, make your activity implement OnClickItem in order to be able to catch clicks in the items in your list.

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • Wow I'm rusty. Is that a whole new class I'm adding in addition to my main activity class and am I getting rid of my loadListView function because of the this new separate class or will they work together? – Mickey D Mar 27 '18 at 00:50
  • Yeah, recycler view is the new API for lists in Android. https://developer.android.com/guide/topics/ui/layout/recyclerview.html. you can use my method instead of yours. – Levi Moreira Mar 27 '18 at 00:53
  • Sorry that was 2 questions. 1) Am I adding that class to my project and 2) Do I get rid of loadListView. As it stands when I add it as a new class in my project it is giving me a red underscore for a problem with this.items = items; (incompatible types) and it's giving me a red underscore error for convertView in the line appName = (TextView) convertView.findViewById(R.id.item_app_name); With your response I'm also reading that article you linked to. That might help me too. Thanks – Mickey D Mar 27 '18 at 00:58
  • Yeah, check the link. You need to create an adapter to go with your recycler view. And you should use the method I've provided. Don't forget to replace the list view in the XML with a recycler view :) – Levi Moreira Mar 27 '18 at 01:01
  • Wow reading that article just tripled the complexity. I am trying to find a correlation between your sample code and that documentation. I'm not seeing where that new class gets convertView hence the error cannot resolve symbol convertView – Mickey D Mar 27 '18 at 01:12
  • Sorry, I had copied from a sample code of mine and forgot to replace some bits, check the new version. In the link I provided there is a class called MyAdapter, in this example I've given you it's called SpecialAdapter. – Levi Moreira Mar 27 '18 at 01:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167590/discussion-between-mickey-d-and-levi-albuquerque). – Mickey D Mar 27 '18 at 01:26
  • 1
    Perfect thanks @Levi. Very interesting how an adapter works. Generally I don't use my LoadListView method anymore but my main activity now calls setUpRecyclerView() instead which takes the data from the apps list array and fills it into the RecyclerView which allows for horizontal scrolling. Note I also had to encase the Recycler view in a linear layout instead of my table layout. Last thing I had to do was give my relative layout in my list_item.xml view a fixed layout_width instead of matching or filling the parent. – Mickey D Mar 27 '18 at 02:49