0

activity_driver_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DriverListActivity">

    <include
        android:id="@+id/toolbarDL"
        layout="@layout/toolbar"/>

    <RelativeLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </RelativeLayout>

</LinearLayout>

fragment_driver_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DLFragment.DriverListFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/driverList"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

raw_driver_list.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:elevation="5dp"
    app:cardCornerRadius="4dp"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:orientation="horizontal">

        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:padding="10dp"
            android:src="@drawable/ic_android"
            app:civ_border_width="2dp"
            app:civ_border_color="@color/quantum_lightgreen"/>

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

            <TextView
                android:id="@+id/carName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Suzuki Abcd"
                android:text="SUZUKI Wagon r"
                android:layout_gravity="center_horizontal"
                android:padding="10dp"
                android:textColor="@color/places_text_black_alpha_87"
                android:textSize="20sp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/driverNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="0312-1234567"
                android:text="0312-1234567"
                android:layout_gravity="center_horizontal"
                android:padding="10dp"
                android:textColor="@color/cardview_dark_background"
                android:textSize="15sp"
                android:textStyle="bold"/>
        </LinearLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

DriverListAdapter.Java

public class DriverListAdapter extends RecyclerView.Adapter<DriverListAdapter.DriverListViewHolder> {

    private ArrayList<Driver> mLst;

    public class DriverListViewHolder extends RecyclerView.ViewHolder{

        public CircleImageView mImage;
        public TextView mCName, mDNumber;

        public DriverListViewHolder(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.profile_image);
            mCName = itemView.findViewById(R.id.carName);
            mDNumber = itemView.findViewById(R.id.driverNumber);
        }
    }

    @NonNull
    @Override
    public DriverListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_driver_list, parent, false);
        DriverListViewHolder driverListAdapter = new DriverListViewHolder(view);
        return driverListAdapter;
    }

    @Override
    public void onBindViewHolder(@NonNull DriverListViewHolder holder, int position) {
        Driver driver = mLst.get(position);
        holder.mImage.setImageResource(driver.getdImage());
        holder.mCName.setText(driver.getdCName());
    }

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

    public DriverListAdapter(ArrayList<Driver> lst) {
        this.mLst = lst;
    }
}

DriverListFragment.Java

public class DriverListFragment extends Fragment {

    public static final String TAG = "DriverListFragment";
    private ArrayList<Driver> driverLst;
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private DriverListAdapter mAdapter;

    public DriverListFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_driver_list, container, false);

        driverLst = new ArrayList<>();
        driverLst.add(new Driver(R.drawable.ic_android,"Car One","0312-1234567"));
        driverLst.add(new Driver(R.drawable.ic_android,"Car Two","0312-1234567"));
        driverLst.add(new Driver(R.drawable.ic_android,"Car Three","0312-1234567"));
        driverLst.add(new Driver(R.drawable.ic_android,"Car Four","0312-1234567"));
        driverLst.add(new Driver(R.drawable.ic_android,"Car Five","0312-1234567"));

        mRecyclerView = (RecyclerView) v.findViewById(R.id.driverList);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this.getActivity());
        mAdapter = new DriverListAdapter(driverLst);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        return v;
    }
}

DriverListActivity.Java

public class DriverListActivity extends AppCompatActivity {

    public static final String TAG = "DriverListActivity";
    private TDD tdd = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_list);
        initToolbar();
        getMyIntent();
        driverList();
    }

    private void getMyIntent()
    {
        Intent i = getIntent();
        tdd = (TDD) i.getSerializableExtra("tdd");
        Log.d(TAG,tdd.toString());
    }

    private void initToolbar()
    {
        Toolbar toolbar = findViewById(R.id.toolbarDL);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Hello There..");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void driverList()
    {
        DriverListFragment driverListFragment = new DriverListFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragmentContainer,driverListFragment);
        fragmentTransaction.addToBackStack("driverListFragment");
        fragmentTransaction.commit();
    }

}

I want to show my RecyclerView list in fragment instead of an activity, when i call fragment in activity it simply showing me blank activity however, there's no error in code. How can i achieve this task or where i am doing a mistake?

Azhar Bhatti
  • 91
  • 1
  • 3
  • 14
  • 1
    In `activity_driver_list`, you need to add `android:orientation="vertical"` to the ``. `LinearLayout`'s have a horizontal orientation by default, and with your toolbar – which presumably has `match_parent` width – the `RecyclerView` is being pushed out the side, off-screen. – Mike M. Apr 10 '20 at 00:23

1 Answers1

0

How to build a Horizontal ListView with RecyclerView?

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view); myList.setLayoutManager(layoutManager);

Jaks Blackhat
  • 87
  • 1
  • 4