FrameLayout不能转换到com.ramotion.foldingcell.FoldingCell

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (81)

我有一个Framelayout,我想在其中有一个Foldercell,但我有这个错误,我不知道如何解决它。

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.xbxvxe.rndfy, PID: 25023
                  java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.ramotion.foldingcell.FoldingCell
                      at com.xbxvxe.rndfy.fragment.FoldingCellListAdapter.getView(FoldingCellListAdapter.java:52)
                      at android.widget.AbsListView.obtainView(AbsListView.java:2365)
                      at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
                      at android.widget.ListView.onMeasure(ListView.java:1315)
                      at android.view.View.measure(View.java:22071)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at android.view.View.measure(View.java:22071)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
                      at android.view.View.measure(View.java:22071)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                      at android.view.View.measure(View.java:22071)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at android.view.View.measure(View.java:22071)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                      at android.view.View.measure(View.java:22071)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
                      at android.view.View.measure(View.java:22071)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                      at android.view.Choreographer.doFrame(Choreographer.java:658)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                      at android.os.Handler.handleCallback(Handler.java:790)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6494)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我在这一行有错误

cell = (FoldingCell) vi.inflate(R.layout.fragment_manageusers, parent, false);

我的类是这样的:

public class FoldingCellListAdapter extends ArrayAdapter<Item> {

    private HashSet<Integer> unfoldedIndexes = new HashSet<>();
    private View.OnClickListener defaultRequestBtnClickListener;

    public FoldingCellListAdapter(Context context, List<Item> objects) {
        super(context, 0, objects);
    }

    @SuppressLint("ResourceType")
    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        // get item for selected view
        Item item = getItem(position);
        // if cell is exists - reuse it, if not - create the new one from resource
        FoldingCell cell = (FoldingCell) convertView;

        ViewHolder viewHolder;
        if (cell == null) {
            viewHolder = new ViewHolder();

            //original
            LayoutInflater vi = LayoutInflater.from(getContext());
            cell = (FoldingCell) vi.inflate(R.layout.fragment_manageusers, parent, false);

            // binding view parts to view holder
            viewHolder.price = cell.findViewById(R.id.title_price);
            viewHolder.time = cell.findViewById(R.id.title_time_label);
            viewHolder.date = cell.findViewById(R.id.title_date_label);
            viewHolder.fromAddress = cell.findViewById(R.id.title_from_address);
            viewHolder.toAddress = cell.findViewById(R.id.title_to_address);
            viewHolder.requestsCount = cell.findViewById(R.id.title_requests_count);
            viewHolder.pledgePrice = cell.findViewById(R.id.title_pledge);
            viewHolder.contentRequestBtn = cell.findViewById(R.id.content_request_btn);
            cell.setTag(viewHolder);
        } else {
            // for existing cell set valid valid state(without animation)
            if (unfoldedIndexes.contains(position)) {
                cell.unfold(true);
            } else {
                cell.fold(true);
            }
            viewHolder = (ViewHolder) cell.getTag();
        }

        if (null == item)
            return cell;

        // bind data from selected element to view through view holder
        viewHolder.price.setText(item.getPrice());
        viewHolder.time.setText(item.getTime());
        viewHolder.date.setText(item.getDate());
        viewHolder.fromAddress.setText(item.getFromAddress());
        viewHolder.toAddress.setText(item.getToAddress());
        viewHolder.requestsCount.setText(String.valueOf(item.getRequestsCount()));
        viewHolder.pledgePrice.setText(item.getPledgePrice());

        // set custom btn handler for list item from that item
        if (item.getRequestBtnClickListener() != null) {
            viewHolder.contentRequestBtn.setOnClickListener(item.getRequestBtnClickListener());
        } else {
            // (optionally) add "default" handler if no handler found in item
            viewHolder.contentRequestBtn.setOnClickListener(defaultRequestBtnClickListener);
        }

        return cell;
    }


    // simple methods for register cell state changes
    public void registerToggle(int position) {
        if (unfoldedIndexes.contains(position))
            registerFold(position);
        else
            registerUnfold(position);
    }

    public void registerFold(int position) {
        unfoldedIndexes.remove(position);
    }

    public void registerUnfold(int position) {
        unfoldedIndexes.add(position);
    }

    public View.OnClickListener getDefaultRequestBtnClickListener() {
        return defaultRequestBtnClickListener;
    }

    public void setDefaultRequestBtnClickListener(View.OnClickListener defaultRequestBtnClickListener) {
        this.defaultRequestBtnClickListener = defaultRequestBtnClickListener;
    }

    // View lookup cache
    private static class ViewHolder {
        TextView price;
        TextView contentRequestBtn;
        TextView pledgePrice;
        TextView fromAddress;
        TextView toAddress;
        TextView requestsCount;
        TextView date;
        TextView time;
    }
}

我的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/fragment_manageusers"
    android:layout_height="wrap_content">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/listUsers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:divider="@android:color/transparent"
        android:dividerHeight="10dp"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:scrollbars="none"
        tools:context=".fragment.ManageusersFragment"/>

    <com.ramotion.foldingcell.FoldingCell xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:folding-cell="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cell"
        folding-cell:additionalFlipsCount="2"
        folding-cell:animationDuration="1300"
        folding-cell:backSideColor="@color/bgBackSideColor"
        folding-cell:cameraHeight="30">

        <!-- CONTENT (UNFOLDED) LAYOUT (MUST BE AT LEAST 2x times BIGGER than content layout bellow)-->
        <include layout="@layout/cell_content_layout" />

        <!-- TITLE (FOLDED) LAYOUT (MUST BE AT LEAST 2x times SMALLER than content layout above) -->
        <include layout="@layout/cell_title_layout" />

    </com.ramotion.foldingcell.FoldingCell>


</FrameLayout>
提问于
用户回答回答于
> java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.ramotion.foldingcell.FoldingCell

- `FoldingCell` is a child of `FrameLayout` you have to inflate `convertView` as `View` then findById from `convertView` to get `FoldingCell`



@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    // get item for selected view
    Item item = getItem(position);
    // if cell is exists - reuse it, if not - create the new one from resource
    // FoldingCell cell = (FoldingCell) convertView; // remove this line...

    final ViewHolder viewHolder;
    if (convertView == null) {
        convertView  = LayoutInflater.from(getContext()).inflate(R.layout.list_item_manageruser, parent, false);
        viewHolder = new ViewHolder();


        // binding view parts to view holder
        viewHolder.foldingCell = convertView.findViewById(R.id.cell);
        viewHolder.price = convertView.findViewById(R.id.title_price);
        viewHolder.time = convertView.findViewById(R.id.title_time_label);
        viewHolder.date = convertView.findViewById(R.id.title_date_label);
        viewHolder.fromAddress = convertView.findViewById(R.id.title_from_address);
        viewHolder.toAddress = convertView.findViewById(R.id.title_to_address);
        viewHolder.requestsCount = convertView.findViewById(R.id.title_requests_count);
        viewHolder.pledgePrice = convertView.findViewById(R.id.title_pledge);
        viewHolder.contentRequestBtn = convertView.findViewById(R.id.content_request_btn);

        convertView.setTag(viewHolder); // bind viewHolder with each view
    }else{
        viewHolder = (ViewHolder)convertView.getTag();
    }
    // Update your UI logic...

   (unfoldedIndexes.contains(position)) { viewHolder.foldingCell.unfold(true);}
}

更新

// View lookup cache
private static class ViewHolder {
    TextView price;
    TextView contentRequestBtn;
    TextView pledgePrice;
    TextView fromAddress;
    TextView toAddress;
    TextView requestsCount;
    TextView date;
    TextView time;
    // Add FoldingCell here
    FoldingCell foldingCell;
}

更新2

  • 拆分fragment_manageusers布局有两种布局。

包含的fragmentusers.xmlListView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/fragment_manageusers"
android:layout_height="wrap_content">

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/listUsers"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:divider="@android:color/transparent"
    android:dividerHeight="10dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:scrollbars="none"
    tools:context=".fragment.ManageusersFragment"/>

 </FrameLayout>

list_item_manageruser.xml,其中包含将使covertView listView项膨胀的列表项行视图。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/fragment_manageusers"
android:layout_height="wrap_content">
<com.ramotion.foldingcell.FoldingCell xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:folding-cell="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cell"
    folding-cell:additionalFlipsCount="2"
    folding-cell:animationDuration="1300"
    folding-cell:backSideColor="@color/bgBackSideColor"
    folding-cell:cameraHeight="30">

    <!-- CONTENT (UNFOLDED) LAYOUT (MUST BE AT LEAST 2x times BIGGER than content layout bellow)-->
    <include layout="@layout/cell_content_layout" />

    <!-- TITLE (FOLDED) LAYOUT (MUST BE AT LEAST 2x times SMALLER than content layout above) -->
    <include layout="@layout/cell_title_layout" />

</com.ramotion.foldingcell.FoldingCell>
 </FrameLayout>

所属标签

可能回答问题的人

  • 应用案例分享

    1 粉丝490 提问19 回答
  • 学生

    3 粉丝476 提问13 回答
  • uncle_light

    5 粉丝518 提问12 回答
  • 最爱开车啦

    8 粉丝503 提问12 回答

扫码关注云+社区

领取腾讯云代金券