Java android.widget.FrameLayout无法强制转换为com.ramotion.foldingcell.foldingcell
我有一个框架布局,我想在其中有一个Foldercell,但我有这个错误,我不知道如何解决它Java android.widget.FrameLayout无法强制转换为com.ramotion.foldingcell.foldingcell,java,android,Java,Android,我有一个框架布局,我想在其中有一个Foldercell,但我有这个错误,我不知道如何解决它 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xbxvxe.rndfy, PID: 25023 java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.ramotion.fo
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>
公共类FoldingCellListAdapter扩展了ArrayAdapter{
私有HashSet unfoldedIndex=新HashSet();
private View.OnClickListener DefaultRequestBtclClickListener;
公共FoldingCellListAdapter(上下文、列表对象){
超级(上下文,0,对象);
}
@SuppressLint(“资源类型”)
@非空
@凌驾
公共视图getView(int位置,视图转换视图,@NonNull视图组父级){
//获取所选视图的项目
项目=获取项目(位置);
//如果单元存在-重用它,如果不存在-从资源创建新的单元
FoldingCell=(FoldingCell)convertView;
持票人持票人;
if(单元格==null){
viewHolder=新的viewHolder();
//原创的
LayoutInflater vi=LayoutInflater.from(getContext());
单元格=(折叠单元格)vi.充气(R.layout.fragment\u manageusers,父级,false);
//将视图零件绑定到视图保持架
viewHolder.price=cell.findviewbyd(R.id.title\u price);
viewHolder.time=cell.findViewById(R.id.title\u time\u标签);
viewHolder.date=cell.findViewById(R.id.title\u date\u标签);
viewHolder.fromAddress=cell.findViewById(R.id.title\u from\u address);
viewHolder.toAddress=cell.findViewById(R.id.title\u to\u地址);
viewHolder.requestscont=cell.findViewById(R.id.title\u requests\u count);
viewHolder.price=cell.findViewById(R.id.title\u质押);
viewHolder.contentRequestBtn=cell.findviewbyd(R.id.content\u request\u btn);
cell.setTag(viewHolder);
}否则{
//对于现有单元集有效状态(无动画)
if(展开索引包含(位置)){
单元展开(真);
}否则{
细胞折叠(真);
}
viewHolder=(viewHolder)cell.getTag();
}
if(null==项)
返回单元;
//通过视图保持器将选定元素中的数据绑定到视图
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.requestscont.setText(String.valueOf(item.getrequestscont());
viewHolder.swarePrice.setText(item.getSwarePrice());
//为该项中的列表项设置自定义btn处理程序
if(item.getRequestBtnClickListener()!=null){
viewHolder.contentRequestBtn.setOnClickListener(item.getRequestBtnClickListener());
}否则{
//(可选)如果在项中未找到处理程序,则添加“默认”处理程序
viewHolder.contentRequestBtn.setOnClickListener(defaultrequestbtnclickllistener);
}
返回单元;
}
//寄存器单元状态更改的简单方法
公共无效寄存器日志(内部位置){
if(展开索引包含(位置))
注册文件夹(位置);
其他的
注册信息(位置);
}
公共无效注册表文件夹(整型位置){
展开索引。移除(位置);
}
公共无效注册表文件夹(整型位置){
展开索引。添加(位置);
}
public View.OnClickListener GetDefaultRequestBtclClickListener(){
返回DefaultRequestBtclListener;
}
public void setDefaultRequestBtclClickListener(View.OnClickListener DefaultRequestBtclListener){
this.defaultRequestBtnClickListener=defaultRequestBtnClickListener;
}
//视图查找缓存
私有静态类视图持有者{
文本视图价格;
文本视图内容请求BTN;
TextView质权价格;
TextView fromAddress;
文本视图地址;
文本视图请求;
文本查看日期;
文本查看时间;
}
}
我的布局是这样的:
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>
更新
// 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\u manageusers
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">
<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\u item\u manageruser.xml包含列表项行视图的列表项,该行视图将膨胀covertView列表视图项
<?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>
此行的含义FoldingCell cell=(FoldingCell)convertView代码>此代码在许多级别上都有很多错误。请再次尝试阅读您正在阅读的教程。@KhaledLela convertView用于重用旧视图,因为正如您所知,适配器允许您使用新数据重用某些视图。它还可以通过不重新创建已经创建的视图来优化性能,因为我已经创建了一个视图,通过始终返回到我的drawer@mudit_sen这不是我要学的教程,我想将折叠单元格视图与我的代码集成在一起,但它与我的代码在很多级别上都不兼容,我不知道如何解决它。这是您在问题中添加的布局文件,并且在列表视图中为该项充气。我无法删除这一行“FoldingCell=(FoldingCell)convertView;”,因为它是一个类,我需要它来获得我在最后使用的折叠和展开运动,就像if(unfoldingDexes.contains(position)){cell.unfold(true);}unfold(boolean)是一个方法,它需要在某个地方声明,这是使用line FoldingCell=(FoldingCell)con