Last active
October 6, 2015 09:22
-
-
Save shihabmi7/f5bb4a44a942fb14d9c1 to your computer and use it in GitHub Desktop.
Pinned Section Listview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DemoFoodListAdapter extends BaseAdapter implements PinnedSectionListAdapter{ | |
// i am following this library @aamir & @sweetwisher | |
// https://github.com/beworker/pinned-section-listview | |
private final Context mContext; | |
ArrayList<Food> foodList; | |
DisplayImageOptions options; | |
ImageLoader imageLoader = ImageLoader.getInstance(); | |
// View Type for Separators | |
private static final int ITEM_VIEW_TYPE_SEPARATOR = 0; | |
// View Type for Regular rows | |
private static final int ITEM_VIEW_TYPE_REGULAR = 1; | |
// Types of Views that need to be handled | |
// -- Separators and Regular rows -- | |
private static final int ITEM_VIEW_TYPE_COUNT = 2; | |
// we use universal image loader library | |
public DemoFoodListAdapter(Context context, ArrayList<Food> list) { | |
mContext = context; | |
this.foodList = list; | |
// =============== UIL Initialization ================= | |
options = new DisplayImageOptions.Builder() | |
.showImageForEmptyUri(R.drawable.ic_launcher) | |
.showImageOnFail(R.drawable.ic_launcher) | |
.displayer(new RoundedBitmapDisplayer(5)).cacheInMemory(true) | |
.build(); | |
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( | |
mContext).diskCacheExtraOptions(480, 480, null).build(); | |
imageLoader.init(config); | |
// ===================================================== | |
} | |
/* | |
* DONE | |
*/@Override | |
public int getCount() { | |
// return 100; | |
return foodList.size(); | |
} | |
@Override | |
public Food getItem(int position) { | |
return foodList.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public boolean isEnabled(int position) { | |
return getItemViewType(position) != ITEM_VIEW_TYPE_SEPARATOR; | |
} | |
@Override | |
public int getViewTypeCount() { | |
return ITEM_VIEW_TYPE_COUNT; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return foodList.get(position).getFoodType(); | |
} | |
/* | |
* | |
* Responsible for Pinned ListView | |
*/ | |
@Override | |
public boolean isItemViewTypePinned(int viewType) { | |
// TODO Auto-generated method stub | |
return viewType == Food.FOOD_SECTION; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder = null; | |
final Food aFood = foodList.get(position); | |
if (convertView == null) { | |
holder = new ViewHolder(); | |
switch (aFood.getFoodType()) { | |
case Food.FOOD_SECTION: | |
convertView = LayoutInflater.from(mContext).inflate( | |
R.layout.item_food_header, parent, false); | |
holder.food_section_header = (TextView) convertView | |
.findViewById(R.id.header); | |
break; | |
case Food.FOOD_ITEM: | |
convertView = LayoutInflater.from(mContext).inflate( | |
R.layout.listview_item_a_food, parent, false); | |
holder.food_name = (TextView) convertView | |
.findViewById(R.id.food_name); | |
holder.food_description = (TextView) convertView | |
.findViewById(R.id.food_description); | |
holder.food_image = (ImageView) convertView | |
.findViewById(R.id.imageView_food_pic); | |
break; | |
} | |
// holder.ratingBar_restaurent = (RatingBar) convertView | |
// .findViewById(R.id.ratingBar_restaurent); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
convertView.setClickable(true); | |
convertView.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
// << this is fired for item >>> | |
Toast.makeText(mContext, "Food Item : " + aFood.getFoodName(), | |
Toast.LENGTH_LONG).show(); | |
} | |
}); | |
switch (aFood.getFoodType()) { | |
case Food.FOOD_SECTION: | |
holder.food_section_header.setText(aFood.getFoodTypeName()); | |
break; | |
case Food.FOOD_ITEM: | |
holder.food_name.setText(aFood.getFoodName()); | |
break; | |
} | |
return convertView; | |
} | |
class ViewHolder { | |
private int catatory_id; | |
private TextView food_name; | |
private TextView food_section_header; | |
private TextView food_description; | |
private ImageView food_image; | |
RatingBar ratingBar_restaurent; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DemoRestaurentFoodListActivity extends Activity { | |
DemoRestaurentFoodListActivity activity = this; | |
// TODO GET THE LIST FROM HALAL HUBS: finished | |
// TODO SET THE LSIT ITEM LAYOUT & ADAPTER : finished | |
// TODO SHOW LIST , implements pin secion list adapter : finish | |
PinnedSectionListView list_view_foodList; | |
// ListView list_view_foodList; | |
private DemoFoodListAdapter foodListAdapter; | |
ArrayList<Food> foodList = new ArrayList<Food>(); | |
private boolean isFastScroll = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
// TODO Auto-generated method stub | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.demo_activity_food_list); | |
list_view_foodList = (PinnedSectionListView) findViewById(R.id.list_view_foodList); | |
// list_view_foodList = (ListView) | |
// findViewById(R.id.list_view_foodList); | |
// list_view_foodList.setClickable(true); | |
// getListView().setFastScrollEnabled(isFastScroll); | |
try { | |
if (InternetConnection.isAvailable(activity)) { | |
getFoodListFromAPI(); | |
} else { | |
showAlert(activity); | |
} | |
list_view_foodList | |
.setOnItemClickListener(new OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> arg0, View arg1, | |
int position, long arg3) { | |
// TODO GO TO NEXT PAGE | |
Food food = foodList.get(position); | |
Toast.makeText(getApplicationContext(), | |
"Food Item : " + food.getFoodTypeName(), | |
Toast.LENGTH_LONG).show(); | |
if (food.getFoodType() == Food.FOOD_ITEM) { | |
// <<< this is not Fired !!!>>>> | |
// do your want to do... | |
Toast.makeText(getApplicationContext(), | |
"Food Item : " + food.getFoodName(), | |
Toast.LENGTH_LONG).show(); | |
} else if (food.getFoodType() == Food.FOOD_SECTION) { | |
// the Section is on click | |
// <<< this is Fired !!!>>>> | |
Toast.makeText(getApplicationContext(), | |
"" + food.getFoodTypeName(), | |
Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
} catch (RuntimeException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void getFoodListFromAPI() { | |
GetFoodListFromAPI api = new GetFoodListFromAPI(activity, true); | |
api.setListener(new CustomListener() { | |
@Override | |
public void ModificationMade() { | |
foodList = ApplicationData.foodList; | |
foodListAdapter = new DemoFoodListAdapter( | |
getApplicationContext(), foodList); | |
list_view_foodList.setAdapter(foodListAdapter); | |
// setListAdapter(foodListAdapter); | |
} | |
}); | |
api.execute(); | |
} | |
public void showAlert(final Activity activity) { | |
if (InternetConnection.isAvailable(activity)) { | |
getFoodListFromAPI(); | |
} else { | |
new AlertDialog.Builder(activity) | |
.setIcon(android.R.drawable.ic_dialog_alert) | |
.setTitle("No Internet Connection") | |
.setMessage("Please check your connectivity.") | |
.setPositiveButton("Exit", | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, | |
int which) { | |
InternetConnection | |
.ExitApplication(activity); | |
} | |
}) | |
.setNegativeButton("Retry", | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, | |
int which) { | |
// Stop the activity | |
showAlert(activity); | |
} | |
}).show(); | |
} | |
} | |
} |
Author
shihabmi7
commented
Oct 6, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment