Hi Guys!
Today I am sharing the important code of fragment list view with image and text. I am using android support library for this fragment.
A
Screen shot in landscape mode:
Cheers Guys!!
Today I am sharing the important code of fragment list view with image and text. I am using android support library for this fragment.
A
Fragment
represents a behavior or a portion of user interface in an Activity
. More detail about the fragment visit the android developer site here Why would we use a fragment?
You can create all your logic in an Activity and then manipulate the screen based on device size and orientation but the amount of code to pro grammatically draw views and implement all the logic based on layout and/or orientation will be make the whole project more complex than it needs to be. This is where fragments come to rescue, you can separate out the logic based on a functional entity rather than how the whole application looks to the end user.
Fragment is available in android support library for more detail visit the android developer site here
Here we are using the cursor adapter for showing the item in list view with help of fragment by using the functionality onCreateView(). So lets start the coding the fragment that managed the portrait and landscape mode. And in details page we can use the the view pager that makes to swipe the item details from left to right. For more details about the View Pager please visit the android developer site here
I am not add to all code but added some code that are important to show this structure.
Please watch the video.Here we are using the cursor adapter for showing the item in list view with help of fragment by using the functionality onCreateView(). So lets start the coding the fragment that managed the portrait and landscape mode. And in details page we can use the the view pager that makes to swipe the item details from left to right. For more details about the View Pager please visit the android developer site here
I am not add to all code but added some code that are important to show this structure.
Screen shot in landscape mode:
layout/activity_main.xml
layout-land/activity_main.xml
fragment_list_selector
fragment_listview.xml
fragment_image_pager.xml
list_row_item.xml
>
CustomArrayAdapter.java
package com.example.fragmentactionbar;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomArrayAdapter extends ArrayAdapter{
private static final String TAG="CustomArrayAdapter";
private final int mLayoutResourceId;
private DataItem[] dataitem1=null;
public CustomArrayAdapter(Context context, int textViewResourceId, DataItem[] dataitem) {
super(context, textViewResourceId, dataitem);
Log.v(TAG, "CustomArrayAdapter call");
mLayoutResourceId = textViewResourceId;
dataitem1=dataitem;
}
@Override
public int getCount() {
Log.v(TAG, "Size of the list is:" +dataitem1.length);
return dataitem1.length;
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ItemViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(mLayoutResourceId, null);
holder = new ItemViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.list_row_image_imageview);
holder.nameText = (TextView) convertView.findViewById(R.id.textView_friendname);
holder.nikeNameText = (TextView) convertView.findViewById(R.id.textview_friendnickname);
convertView.setTag(holder);
}
else {
holder = (ItemViewHolder) convertView.getTag();
}
DataItem item=getItem(position);
String name = item.getName();
int imageResId = item.getImageResId();
String nikename =item.getNickname();
Log.v(TAG, "name is: "+name);
Log.v(TAG, "nick name is: "+ nikename);
// Get a smaller version of the image to display in the list for less memory usage
Resources res = getContext().getResources();
int dimensInPixels = res.getDimensionPixelSize(R.dimen.list_row_image_item_dimensions);
Bitmap sampledBitmap = ImageUtils.decodeSampledBitmapFromResource(getContext().getResources(),
imageResId, dimensInPixels, dimensInPixels);
// Update the views
holder.nameText.setText(name);
holder.imageView.setImageBitmap(sampledBitmap);
holder.nikeNameText.setText("Nick Name: "+nikename);
return convertView;
}
private class ItemViewHolder {
public ImageView imageView;
public TextView nameText;
public TextView nikeNameText;
}
}
ItemListFragment.java
package com.example.fragmentactionbar;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class ItemListFragment extends Fragment implements OnItemClickListener, ImageSelector{
private static final String TAG="ItemListFragment";
private ListView mListView;
private CustomArrayAdapter cursorArrayAdapter;
private OnItemSelectedListener mParentOnItemSelectedListener;
private Boolean status;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate: savedInstanceState " + (savedInstanceState == null ? "ture" : "false") + " null");
if (savedInstanceState != null) {
status = false;
}
else {
status = true;
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.v(TAG+"onAttach", "onAttach call");
Fragment parentFragment = getParentFragment();
if (parentFragment != null && parentFragment instanceof OnItemSelectedListener) {
mParentOnItemSelectedListener = (OnItemSelectedListener) parentFragment;
}
else if (activity != null && activity instanceof OnItemSelectedListener) {
mParentOnItemSelectedListener = (OnItemSelectedListener) activity;
}
else if (mParentOnItemSelectedListener == null) {
Log.w(TAG, "onAttach: niether the parent fragment or parent activity implement OnImageSelectedListener, "
+ "image selections will not be communicated to other components");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v(TAG, "onCreateView: savedInstanceState " + (savedInstanceState == null ? "true" : "false") + " null");
View v = inflater.inflate(R.layout.farfment_listview, container, false);
mListView = (ListView) v.findViewById(R.id.fragment_image_list_listview);
mListView.setOnItemClickListener(this);
cursorArrayAdapter=new CustomArrayAdapter(getActivity(), R.layout.list_row_item, StaticItemData.getImageItemArrayInstance());
mListView.setAdapter(cursorArrayAdapter);
mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
// If first creation of fragment, select the first item
if (status && cursorArrayAdapter.getCount() > 0) {
// Default the selection to the first item
mListView.setItemChecked(0, true);
}
// initial onCreate
if (status) {
status = false;
}
return v;
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int position, long id) {
Log.d(TAG, "onItemClick: " + "pos: " + position + " id: " + id);
// Highlight the selected row
mListView.setItemChecked(position, true);
if (mParentOnItemSelectedListener != null) {
DataItem dataItem= cursorArrayAdapter.getItem(position);
mParentOnItemSelectedListener.onItemSelected(dataItem, position);
}
}
@Override
public void setImageSelected(DataItem dataItem, int position) {
if (isResumed()) {
// If the selected position is valid, and different than what is
// currently selected, highlight that row in the list and
// scroll to it
if (position >= 0 && position < cursorArrayAdapter.getCount()
&& position != mListView.getCheckedItemPosition()) {
Log.d(TAG, "setImageSelected: title = " + dataItem.getName() + " position = " + position);
// Highlight the selected row and scroll to it
mListView.setItemChecked(position, true);
mListView.smoothScrollToPosition(position);
}
}
}
}
ItemPagerFragment.java
package com.example.fragmentactionbar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ItemPagerFragment extends Fragment implements OnPageChangeListener, ImageSelector{
private static final String TAG="ItemPagerFragment";
private ViewPager mViewPager;
private ItemPagerAdapter mImagePagerAdapter;
private OnItemSelectedListener mParentOnImageSelectedListener;
private DataItem[] mPagerImageItems;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.v(TAG, "onAttach");
// Check if parent fragment (if there is one) implements the image
// selection interface
Fragment parentFragment = getParentFragment();
if (parentFragment != null && parentFragment instanceof OnItemSelectedListener) {
mParentOnImageSelectedListener = (OnItemSelectedListener) parentFragment;
}
// Otherwise, check if parent activity implements the image
// selection interface
else if (activity != null && activity instanceof OnItemSelectedListener) {
mParentOnImageSelectedListener = (OnItemSelectedListener) activity;
}
// If neither implements the image selection callback, warn that
// selections are being missed
else if (mParentOnImageSelectedListener == null) {
Log.w(TAG, "getting null");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate: savedInstanceState " + (savedInstanceState == null ? "==" : "!=") + " null");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v(TAG, "onCreateView: savedInstanceState " + (savedInstanceState == null ? "==" : "!=") + " null");
// Inflate the fragment main view in the container provided
View v = inflater.inflate(R.layout.fragment_image_pager, container, false);
// Setup views
mViewPager = (ViewPager) v.findViewById(R.id.fragment_image_pager_viewpager);
mPagerImageItems = StaticItemData.getImageItemArrayInstance();
mImagePagerAdapter = new ItemPagerAdapter(mPagerImageItems);
mViewPager.setAdapter(mImagePagerAdapter);
// Listen for page changes to update other views
mViewPager.setOnPageChangeListener(this);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.v(TAG, "onViewCreated");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.v(TAG, "onActivityCreated");
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
Log.v(TAG, "onViewStateRestored");
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.d(TAG, "onPageSelected: " + position);
// Inform our parent listener that an image was selected
if (mParentOnImageSelectedListener != null) {
mParentOnImageSelectedListener.onItemSelected(mPagerImageItems[position], position);
}
}
@Override
public void setImageSelected(DataItem dataitem, int position) {
if (isResumed()) {
// If the selected position is valid, and different than what is
// currently selected, move the pager to that image
if (position >= 0 && position < mImagePagerAdapter.getCount()
&& position != mViewPager.getCurrentItem()) {
Log.d(TAG, "setImageSelected: title = " + dataitem.getName() + " position = " + position);
// Move the view pager to the current image
mViewPager.setCurrentItem(position, true);
}
}
}
}
MainActivity.java
package com.example.fragmentactionbar;Screen shot in portrait mode:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.ViewGroup;
public class MainActivity extends FragmentActivity {
private static final String TAG="MainActivity";
private ViewGroup framelayoutmain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate: savedInstanceState " + (savedInstanceState == null ? "true" : "false") + " null");
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
}
else {
framelayoutmain = (ViewGroup) findViewById(R.id.activity_main);
if (framelayoutmain != null) {
Log.i(TAG, "onCreate: adding framelayoutmain to MainActivity");
// Add image selector fragment to the activity's container layout
ItemSelectorFragment imageSelectorFragment = new ItemSelectorFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(framelayoutmain.getId(), imageSelectorFragment,ItemSelectorFragment.class.getName());
// Commit the transaction
fragmentTransaction.commit();
}
}
}
}
Cheers Guys!!
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :