Java tutorial
//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2016 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //------------------------------------------------------------------------- package com.pdftron.pdf.controls; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.support.v4.app.DialogFragment; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.RadioButton; import android.widget.TextView; import com.pdftron.pdf.tools.R; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link UserCropSelectionDialogFragment.UserCropSelectionDialogFragmentListener} interface * to handle interaction events. * Use the {@link UserCropSelectionDialogFragment#newInstance} factory method to * create an instance of this fragment. */ public class UserCropSelectionDialogFragment extends DialogFragment { public static final int MODE_AUTO_CROP = 0; public static final int MODE_MANUAL_CROP = 1; public static final int MODE_RESET_CROP = 2; private static final String KEY_ITEM_TEXT = "item_crop_mode_picker_list_text"; public interface UserCropSelectionDialogFragmentListener { void onUserCropMethodSelected(int cropMode); void onUserCropSelectionDialogFragmentDismiss(); } private UserCropSelectionDialogFragmentListener mListener; private ListView mCropModeListView; public static UserCropSelectionDialogFragment newInstance() { UserCropSelectionDialogFragment fragment = new UserCropSelectionDialogFragment(); return fragment; } public UserCropSelectionDialogFragment() { // Required empty public constructor } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (UserCropSelectionDialogFragmentListener) activity; } catch (ClassCastException e) { throw new ClassCastException( activity.toString() + " must implement UserCropSelectionDialogFragmentListener"); } } @Override public void onDetach() { super.onDetach(); } @Override public void onDismiss(DialogInterface dialog) { mListener.onUserCropSelectionDialogFragmentDismiss(); super.onDismiss(dialog); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.fragment_user_crop_selection_dialog, null); builder.setView(view); mCropModeListView = (ListView) view.findViewById(R.id.fragment_user_crop_slection_dialog_listview); mCropModeListView.setItemsCanFocus(false); CropModeEntryAdapter cropModeEntryAdapter = new CropModeEntryAdapter(getActivity(), getViewModeList()); mCropModeListView.setAdapter(cropModeEntryAdapter); mCropModeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mListener.onUserCropMethodSelected(position); } }); builder.setPositiveButton(getActivity().getResources().getString(R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dismiss(); } }); return builder.create(); } private class CropModeEntryAdapter extends ArrayAdapter<HashMap<String, Object>> { private Context mContext; private List<HashMap<String, Object>> mEntries; public CropModeEntryAdapter(Context context, List<HashMap<String, Object>> entries) { super(context, 0, entries); this.mContext = context; this.mEntries = entries; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { convertView = View.inflate(mContext, R.layout.listview_item_view_mode_picker_list, null); holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.item_view_mode_picker_list_icon); holder.text = (TextView) convertView.findViewById(R.id.item_view_mode_picker_list_text); holder.radioButton = (RadioButton) convertView .findViewById(R.id.item_view_mode_picker_list_radiobutton); holder.switchButton = (com.pdftron.pdf.widget.InertSwitch) convertView .findViewById(R.id.item_view_mode_picker_list_switch); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } HashMap<String, Object> map = this.mEntries.get(position); holder.icon.setVisibility(View.GONE); holder.text.setText((String) map.get(KEY_ITEM_TEXT)); holder.radioButton.setVisibility(View.GONE); holder.switchButton.setVisibility(View.GONE); return convertView; } private class ViewHolder { protected ImageView icon; protected TextView text; protected RadioButton radioButton; protected com.pdftron.pdf.widget.InertSwitch switchButton; } } private List<HashMap<String, Object>> getViewModeList() { List<HashMap<String, Object>> viewModesList = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> mapSinglePage = new HashMap<String, Object>(); mapSinglePage.put(KEY_ITEM_TEXT, getActivity().getResources().getString(R.string.user_crop_selection_auto_crop)); viewModesList.add(mapSinglePage); HashMap<String, Object> mapFacing = new HashMap<String, Object>(); mapFacing.put(KEY_ITEM_TEXT, getActivity().getResources().getString(R.string.user_crop_selection_manual_crop)); viewModesList.add(mapFacing); HashMap<String, Object> mapFacingCover = new HashMap<String, Object>(); mapFacingCover.put(KEY_ITEM_TEXT, getActivity().getResources().getString(R.string.user_crop_selection_remove_crop)); viewModesList.add(mapFacingCover); return viewModesList; } }