Download libs
Here
class BasePanel extends FrameLayout { ViewGroup mPanelContainer; //Constructors BasePanel(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPanelContainer(); } /* ***************************************************************************** */
/* ******************************** Utility API ******************************** */
/* ***************************************************************************** */ private void initPanelContainer() { LayoutInflater inflater = LayoutInflater.from(getContext()); mPanelContainer = (ViewGroup) inflater.inflate(R.layout.carousel_base_panel, this, true); } }
public class ImagePanel extends BasePanel { private ImageView mImageViewHolder; //Constructors public ImagePanel(Context context) { this(context, null); } public ImagePanel(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ImagePanel(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initImagePanel(); } public void setImageResId(int resId) { mImageViewHolder.setImageResource(resId); } /* ***************************************************************************** */
/* ******************************** Utility API ******************************** */
/* ***************************************************************************** */ private void initImagePanel() { mImageViewHolder = new ImageView(getContext()); mImageViewHolder.setScaleType(ImageView.ScaleType.FIT_XY); mImageViewHolder.setImageResource(R.mipmap.ic_launcher); LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); mImageViewHolder.setLayoutParams(lp); mPanelContainer.addView(mImageViewHolder); } }
public class LayoutPanel extends BasePanel { //Constructors public LayoutPanel(Context context) { this(context, null); } public LayoutPanel(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LayoutPanel(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initLayoutPanel(); } /* ***************************************************************************** */
/* ******************************** Utility API ******************************** */
/* ***************************************************************************** */ private void initLayoutPanel() { View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_panel, null); view.findViewById(R.id.click_me_button).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { Toast.makeText(getContext(), "Button click", Toast.LENGTH_SHORT).show(); } }); mPanelContainer.addView(view); } }
public class ListLayoutPanel extends BasePanel { public interface OnScrollListener { void onScroll(); } private OnScrollListener mOnScrollListener; private String[] values = new String[] { "Iron Man", "Thor", "Hulk", "Captain America", "Black Widow", "Hawkeye", "Quicksilver", "Scarlet Witch", "Vision", "Maria Hill", "Falcon", "Ultron"}; //Constructors public ListLayoutPanel(Context context) { this(context, null); } public ListLayoutPanel(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ListLayoutPanel(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initLayoutPanel(); } public void setOnScrollListener(OnScrollListener onScrollListener) { mOnScrollListener = onScrollListener; } /* ***************************************************************************** */
/* ******************************** Utility API ******************************** */
/* ***************************************************************************** */ private void initLayoutPanel() { View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_list_panel, null); ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, values); final ListView listView = (ListView) view.findViewById(R.id.listView); listView.setAdapter(adapter); listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override
public void onScrollStateChanged(AbsListView view, int scrollState) { if(mOnScrollListener != null) { mOnScrollListener.onScroll(); } } @Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(mOnScrollListener != null) { mOnScrollListener.onScroll(); } } }); mPanelContainer.addView(view); } }
public class CarouselFragment extends Fragment implements ListLayoutPanel.OnScrollListener { public static CarouselFragment newInstance() { Bundle args = new Bundle(); CarouselFragment fragment = new CarouselFragment(); fragment.setArguments(args); return fragment; } private CarouselView mCarouselView; @Nullable @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_carousel, null, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mCarouselView = (CarouselView) view.findViewById(R.id.carouselView); for (View stubItem : initStubItems()) { mCarouselView.addView(stubItem); } mCarouselView.notifyDataSetChanged(); view.findViewById(R.id.prev).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = mCarouselView.getSelectedItemPosition() == 0 ? mCarouselView.getCount() - 1 : mCarouselView.getSelectedItemPosition() - 1; mCarouselView.scrollToChild(position); mCarouselView.invalidate(); } }); view.findViewById(R.id.next).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = mCarouselView.getSelectedItemPosition() == mCarouselView.getCount() - 1 ? 0 : mCarouselView.getSelectedItemPosition() + 1; mCarouselView.scrollToChild(position); mCarouselView.invalidate(); } }); } // Stub items private List<View> initStubItems() { List<View> result = new ArrayList<>(); ImagePanel imagePanel = new ImagePanel(getActivity()); imagePanel.setImageResId(R.drawable.iron_man); result.add(imagePanel); imagePanel = new ImagePanel(getActivity()); imagePanel.setImageResId(R.drawable.natasha); result.add(imagePanel); imagePanel = new ImagePanel(getActivity()); imagePanel.setImageResId(R.drawable.tor); result.add(imagePanel); LayoutPanel layoutPanel = new LayoutPanel(getActivity()); result.add(layoutPanel); ListLayoutPanel listLayoutPanel = new ListLayoutPanel(getActivity()); listLayoutPanel.setOnScrollListener(this); result.add(listLayoutPanel); return result; } @Override public void onScroll() { mCarouselView.invalidate(); } }
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.container, CarouselFragment.newInstance()); fragmentTransaction.commit(); } }
activity_main
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#91000000"
android:orientation="vertical" />
carousel_base_panel
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="match_parent"> </FrameLayout>
fragment_carousel
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#91000000"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/carousel_view_test_application"
android:gravity="center"
android:padding="20dp"
android:textColor="@android:color/white"
android:id="@+id/textView"/> <com.carousel.CarouselView
android:id="@+id/carouselView"
android:layout_width="match_parent"
android:layout_height="300dp"/> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent
android:paddingTop="10dp"
android:layout_height="match_parent"
android:layout_weight="1"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:id="@+id/prev"
android:layout_weight="1"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/next"
android:layout_weight="1"/> </LinearLayout> </LinearLayout>
layout_list_panel
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center_horizontal"/> </LinearLayout>
layout_panel
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me"
android:id="@+id/click_me_button"
android:layout_gravity="center_horizontal"/> </LinearLayout>
No comments:
Post a Comment