如何在Swipcard上设置图像

时间:2015-02-25 07:22:47

标签: android

我试过这样但不幸的是申请已被停止 我正在制作像Tinder一样的Swipcard我创建了一个演示 它在TextView From Arraylist上显示文本

我希望显示图像而不是文本...但应用程序崩溃了 我给出了完整的类代码.. logcat错误

这是MY Class我在哪里添加图像数组。

 LetsBouncedFragment extends SherlockFragment {
      private ArrayList<String> al;
      List<RowItem> rowItems;
        private int i;
ProgressDialog mProgressDialog;
CustomListViewAdapter adapter;
public static final Integer[] images = {R.drawable.ic_launcher,R.drawable.ico_download};
        @InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        RelativeLayout wrapper = new RelativeLayout(getActivity());
        // for example
         inflater.inflate(R.layout.letsbounced, wrapper, true);
         flingContainer=(SwipeFlingAdapterView)wrapper.findViewById(R.id.frame);

          Button left=(Button)wrapper.findViewById(R.id.left);
            Button right=(Button)wrapper.findViewById(R.id.right);
            left.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View wrapper) {
                    // TODO Auto-generated method stub
                    left();
                }
            });
     right.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View wrapper) {
                    // TODO Auto-generated method stub
                    right();
                }
            });
            ButterKnife.inject(getActivity());
          //  new DownloadJSON().execute();
            rowItems = new ArrayList<RowItem>();
            for (int i = 0; i < images.length; i++) {
                RowItem item = new RowItem();
                item.setImageId(images[i]);
                rowItems.add(item);
            }

            adapter = new CustomListViewAdapter(getActivity(),
                    R.layout.item, rowItems);
            flingContainer.setAdapter(adapter);



            flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
                @Override
                public void removeFirstObjectInAdapter() {
                    // this is the simplest way to delete an object from the Adapter (/AdapterView)
                    Log.d("LIST", "removed object!");
                    al.remove(0);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onLeftCardExit(Object dataObject) {
                    //Do something on the left!
                    //You also have access to the original object.
                    //If you want to use it just cast it (String) dataObject
                    Toast.makeText(getActivity(), "Left", 1000).show();
                }

                @Override
                public void onRightCardExit(Object dataObject) {
                    Toast.makeText(getActivity(), "Right", 1000).show();
                }

                @Override
                public void onAdapterAboutToEmpty(int itemsInAdapter) {
                    // Ask for more data here
                    al.add("XML ".concat(String.valueOf(i)));
                    adapter.notifyDataSetChanged();
                    Log.d("LIST", "notified");
                    i++;
                }

                @Override
                public void onScroll(float scrollProgressPercent) {
                    View view = flingContainer.getSelectedView();
                    view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
                    view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
                }
            });


            // Optionally add an OnItemClickListener
            flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
                @Override
                public void onItemClicked(int itemPosition, Object dataObject) {
                    Toast.makeText(getActivity(), "Clicked", 1000).show();
                  //  makeToast(getActivity(), "Clicked!");
                }
            });

            return wrapper;
    }

这是我的适配器

 public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

        Context context;

        public CustomListViewAdapter(Context context, int resourceId,
                List<RowItem> items) {
            super(context, resourceId, items);
            this.context = context;
        }

        /*private view holder class*/
        private class ViewHolder {
            ImageView imageView;
            TextView txtTitle;
            TextView txtDesc;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            RowItem rowItem = getItem(position);

            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item, null);
                holder = new ViewHolder();

                holder.imageView = (ImageView) convertView.findViewById(R.id.helloText);
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();


    holder.imageView.setImageResource(rowItem.getImageId());


            return convertView;
        }
    }

这里是lestbounced布局代码

<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >

<com.example.bounced.card.SwipeFlingAdapterView
    android:id="@+id/frame"
    android:background="#ffeee9e2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:rotation_degrees="15.5"
    tools:context="com.example.bounced.SlidingMenuActivity" />

<include layout="@layout/buttons" />

这是我的SwipFlingAdaptoerView类

public class SwipeFlingAdapterView extends BaseFlingAdapterView {


private int MAX_VISIBLE = 4;
private int MIN_ADAPTER_STACK = 6;
private float ROTATION_DEGREES = 15.f;

private Adapter mAdapter;
private int LAST_OBJECT_IN_STACK = 0;
private onFlingListener mFlingListener;
private AdapterDataSetObserver mDataSetObserver;
private boolean mInLayout = false;
private View mActiveCard = null;
private OnItemClickListener mOnItemClickListener;
private FlingCardListener flingCardListener;


public SwipeFlingAdapterView(Context context) {
    this(context, null);
}

public SwipeFlingAdapterView(Context context, AttributeSet attrs) {
    this(context, attrs, R.attr.SwipeFlingStyle);
}

public SwipeFlingAdapterView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeFlingAdapterView, defStyle, 0);
    MAX_VISIBLE = a.getInt(R.styleable.SwipeFlingAdapterView_max_visible, MAX_VISIBLE);
    MIN_ADAPTER_STACK = a.getInt(R.styleable.SwipeFlingAdapterView_min_adapter_stack, MIN_ADAPTER_STACK);
    ROTATION_DEGREES = a.getFloat(R.styleable.SwipeFlingAdapterView_rotation_degrees, ROTATION_DEGREES);
    a.recycle();
}


/**
 * A shortcut method to set both the listeners and the adapter.
 *
 * @param context The activity context which extends onFlingListener, OnItemClickListener or both
 * @param mAdapter The adapter you have to set.
 */
public void init(final Context context, Adapter mAdapter) {
    if(context instanceof onFlingListener) {
        mFlingListener = (onFlingListener) context;
    }else{
        throw new RuntimeException("Activity does not implement SwipeFlingAdapterView.onFlingListener");
    }
    if(context instanceof OnItemClickListener){
        mOnItemClickListener = (OnItemClickListener) context;
    }
    setAdapter(mAdapter);
}

@Override
public View getSelectedView() {
    return mActiveCard;
}


@Override
public void requestLayout() {
    if (!mInLayout) {
        super.requestLayout();
    }
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    // if we don't have an adapter, we don't need to do anything
    if (mAdapter == null) {
        return;
    }

    mInLayout = true;
    final int adapterCount = mAdapter.getCount();

    if(adapterCount == 0) {
        removeAllViewsInLayout();
    }else {
        View topCard = getChildAt(LAST_OBJECT_IN_STACK);
        if(mActiveCard!=null && topCard!=null && topCard==mActiveCard) {
            removeViewsInLayout(0, LAST_OBJECT_IN_STACK);
            layoutChildren(1, adapterCount);
        }else{
            // Reset the UI and set top view listener
            removeAllViewsInLayout();
            layoutChildren(0, adapterCount);
            setTopView();
        }
    }

    mInLayout = false;

    if(adapterCount < MAX_VISIBLE) mFlingListener.onAdapterAboutToEmpty(adapterCount);
}


private void layoutChildren(int startingIndex, int adapterCount){
    while (startingIndex < Math.min(adapterCount, MAX_VISIBLE) ) {
        View newUnderChild = mAdapter.getView(startingIndex, null, this);
        if (newUnderChild.getVisibility() != GONE) {
            makeAndAddView(newUnderChild);
            LAST_OBJECT_IN_STACK = startingIndex;
        }
        startingIndex++;
    }
}


@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void makeAndAddView(View child) {

    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) child.getLayoutParams();
    addViewInLayout(child, 0, lp, true);

    final boolean needToMeasure = child.isLayoutRequested();
    if (needToMeasure) {
        int childWidthSpec = getChildMeasureSpec(getWidthMeasureSpec(),
                getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin,
                lp.width);
        int childHeightSpec = getChildMeasureSpec(getHeightMeasureSpec(),
                getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin,
                lp.height);
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }


    int w = child.getMeasuredWidth();
    int h = child.getMeasuredHeight();

    int gravity = lp.gravity;
    if (gravity == -1) {
        gravity = Gravity.TOP | Gravity.START;
    }


    int layoutDirection = getLayoutDirection();
    final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
    final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

    int childLeft;
    int childTop;
    switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.CENTER_HORIZONTAL:
            childLeft = (getWidth() + getPaddingLeft() - getPaddingRight()  - w) / 2 +
                    lp.leftMargin - lp.rightMargin;
            break;
        case Gravity.END:
            childLeft = getWidth() + getPaddingRight() - w - lp.rightMargin;
            break;
        case Gravity.START:
        default:
            childLeft = getPaddingLeft() + lp.leftMargin;
            break;
    }
    switch (verticalGravity) {
        case Gravity.CENTER_VERTICAL:
            childTop = (getHeight() + getPaddingTop() - getPaddingBottom()  - h) / 2 +
                    lp.topMargin - lp.bottomMargin;
            break;
        case Gravity.BOTTOM:
            childTop = getHeight() - getPaddingBottom() - h - lp.bottomMargin;
            break;
        case Gravity.TOP:
        default:
            childTop = getPaddingTop() + lp.topMargin;
            break;
    }

    child.layout(childLeft, childTop, childLeft + w, childTop + h);
}




/**
*  Set the top view and add the fling listener
*/
private void setTopView() {
    if(getChildCount()>0){

        mActiveCard = getChildAt(LAST_OBJECT_IN_STACK);
        if(mActiveCard!=null) {

            flingCardListener = new FlingCardListener(mActiveCard, mAdapter.getItem(0),
                    ROTATION_DEGREES, new FlingCardListener.FlingListener() {

                        @Override
                        public void onCardExited() {
                            mActiveCard = null;
                            mFlingListener.removeFirstObjectInAdapter();
                        }

                        @Override
                        public void leftExit(Object dataObject) {
                            mFlingListener.onLeftCardExit(dataObject);
                        }

                        @Override
                        public void rightExit(Object dataObject) {
                            mFlingListener.onRightCardExit(dataObject);
                        }

                        @Override
                        public void onClick(Object dataObject) {
                            if(mOnItemClickListener!=null)
                                mOnItemClickListener.onItemClicked(0, dataObject);

                        }

                        @Override
                        public void onScroll(float scrollProgressPercent) {
                            mFlingListener.onScroll(scrollProgressPercent);
                        }
                    });

            mActiveCard.setOnTouchListener(flingCardListener);
        }
    }
}

public FlingCardListener getTopCardListener() throws NullPointerException{
    if(flingCardListener==null){
        throw new NullPointerException();
    }
    return flingCardListener;
}

public void setMaxVisible(int MAX_VISIBLE){
    this.MAX_VISIBLE = MAX_VISIBLE;
}

public void setMinStackInAdapter(int MIN_ADAPTER_STACK){
    this.MIN_ADAPTER_STACK = MIN_ADAPTER_STACK;
}

@Override
public Adapter getAdapter() {
    return mAdapter;
}


@Override
public void setAdapter(Adapter adapter) {
    if (mAdapter != null && mDataSetObserver != null) {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
        mDataSetObserver = null;
    }

    mAdapter = adapter;

    if (mAdapter != null  && mDataSetObserver == null) {
        mDataSetObserver = new AdapterDataSetObserver();
        mAdapter.registerDataSetObserver(mDataSetObserver);
    }
}

public void setFlingListener(onFlingListener onFlingListener) {
    this.mFlingListener = onFlingListener;
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener){
    this.mOnItemClickListener = onItemClickListener;
}




@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new FrameLayout.LayoutParams(getContext(), attrs);
}


private class AdapterDataSetObserver extends DataSetObserver {
    @Override
    public void onChanged() {
        requestLayout();
    }

    @Override
    public void onInvalidated() {
        requestLayout();
    }

}


public interface OnItemClickListener {
    public void onItemClicked(int itemPosition, Object dataObject);
}

public interface onFlingListener {
    public void removeFirstObjectInAdapter();
    public void onLeftCardExit(Object dataObject);
    public void onRightCardExit(Object dataObject);
    public void onAdapterAboutToEmpty(int itemsInAdapter);
    public void onScroll(float scrollProgressPercent);
}

}

这是我的Logcate给出错误

    02-25 06:16:26.544: E/AndroidRuntime(1279): FATAL EXCEPTION: main
02-25 06:16:26.544: E/AndroidRuntime(1279): java.lang.NullPointerException
02-25 06:16:26.544: E/AndroidRuntime(1279):     at com.example.bounced.card.SwipeFlingAdapterView.makeAndAddView(SwipeFlingAdapterView.java:143)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at com.example.bounced.card.SwipeFlingAdapterView.layoutChildren(SwipeFlingAdapterView.java:126)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at com.example.bounced.card.SwipeFlingAdapterView.onLayout(SwipeFlingAdapterView.java:111)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.View.layout(View.java:13754)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.View.layout(View.java:13754)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.View.layout(View.java:13754)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.View.layout(View.java:13754)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:653)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.View.layout(View.java:13754)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.View.layout(View.java:13754)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
02-25 06:16:26.544: E/AndroidRuntime(1279):     at android.view.View.layout(View.java:13754)

错误给出空指针 在此功能

 if (newUnderChild.getVisibility() != GONE) {
            makeAndAddView(newUnderChild);
            LAST_OBJECT_IN_STACK = startingIndex;
        }

在此行

makeAndAddView(newUnderChild);

0 个答案:

没有答案