Saturday 2 November 2013

Android : The scroll list tutorial

For make scrollable list fragment in Android!
MainActivity class is hosting the ScrollFragment class which extends ListFragment.
The commented code in public void onCreate(Bundle savedInstanceState) method is usable when we wanna just show the list of the strings. To achieve that you have to override the toString() method in Item class.

If we wanna intersect the on click listener we override method public void onListItemClick(ListView l, View v, int position, long id), and in layout (xml) set all focusable objects to  android:focusable="false".

With CostumiseAdapter which extends the ArrayAdapter<T>, is possible to display also other stuff.
You can get all code include xml files in Peter Gostincar Android.
This is the result of the following code.




package com.mamutek.com.contact.intent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;

/**
 * @author Peter Gostincar
 */

public class MainActivity extends FragmentActivity {

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

  FragmentManager fm = getSupportFragmentManager();
  Fragment f = fm.findFragmentById(R.id.fragmentContainer);

  if(f == null){
   f = new MainFragment();
   fm.beginTransaction()
   .add(R.id.fragmentContainer, f)
   .commit();
  }
 }
}

package com.mamutek.scroll.list.android;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * @author Peter Gostincar
 */

public class ScrollFragment extends ListFragment{

 private ArrayList mItems;

 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  getActivity().setTitle("Title");
  mItems = createSomeItems();
  
  //*****this will show only Strings *****//
//  ArrayAdapter adapter = new ArrayAdapter
//         (getActivity(),
//         android.R.layout.simple_list_item_1,
//         mItems);
  CostumiseAdapter adapter = new CostumiseAdapter(mItems);
  setListAdapter(adapter);
 }
 
 @Override
 public void onListItemClick(ListView l, View v, int position, long id){
  Item item = (Item)(getListAdapter()).getItem(position);
  Log.d("TRA" , item.toString());
  //the item is clicked Object on the list...
 }

 private ArrayList createSomeItems() {
  ArrayList ret  = new ArrayList();
  for(int i = 0; i < 30; i++){
   ret.add(new Item(i));
  }
  return ret;
 }
 
 /**
  * @author Peter Gostincar
  * costumise adapter to show more than just Strings
  */
 private class CostumiseAdapter extends ArrayAdapter{

  public CostumiseAdapter(ArrayList items) {
   super(getActivity(), 0, items);
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent){
   if(convertView == null){
    convertView = getActivity().getLayoutInflater().inflate(R.layout.scroll_fragment, null);
   }
   
   Item item = getItem(position);
   
   TextView text = (TextView) convertView.findViewById(R.id.text_view);
   text.setText(item.mString);
   
   RadioButton b1 = (RadioButton) convertView.findViewById(R.id.radio_b1);
   b1.setChecked(item.mB1);
   
   RadioButton b2 = (RadioButton) convertView.findViewById(R.id.radio_b2);
   b2.setChecked(item.mB2);
   
   CheckBox cB = (CheckBox) convertView.findViewById(R.id.check_box);
   cB.setChecked(item.mB2);

   return convertView;
  }
 }

 /**
  * @author Peter Gostincar
  * class just for creating list of objects
  */
 private class Item{
  
  String mString = "Tralalal: ";
  boolean mCheckBox;
  boolean mB1;
  boolean mB2;
  
  public Item(int i) {
   mString += i;
   if(i % 2 == 0){
    mCheckBox = true;
   }
   int x = (int) (Math.random()*3+1);
   if(x == 1)
    mB1 = true;
   if(x == 2)
    mB2 = true;
  }
  
  @Override
  public String toString(){
   return mString;
  }
 }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <CheckBox
        android:id="@+id/check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:focusable="false" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/check_box"
        android:padding="8dp" />

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio_b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_one"
            android:focusable="false" />

        <RadioButton
            android:id="@+id/radio_b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_two"
            android:focusable="false" />

    </RadioGroup>

</RelativeLayout>

No comments:

Post a Comment