- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 Activity 可以打开带有 View 寻呼机的 fragment 。当我在选项卡之间切换时,出现此错误:指定的子项已有父项。您必须先对 child 的 parent 调用 removeView()。
我认为问题出在 RecyclerView 的标题上,我发布了一个关于它的新问题 here
the view pager code is:
private void mViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new a_Frag(), getString("A"));
adapter.addFragment(new b_Frag(), getString("B"));
adapter.addFragment(new c_Frag(), getString("C"));
viewPager.setAdapter(adapter);
}
the a_frag code as example:
public class a_Frag extends Fragment {
RecyclerView recyclerView;
public a_Frag() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.rv, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new a_Adapter(getContext(), R.layout.group_header));
return recyclerView;
}
ExpandableRecyclerView code:
public class ExpandableRecyclerView extends RecyclerView {
public ExpandableRecyclerView(Context context) {
super(context, null);
initRecycler();
}
public ExpandableRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
initRecycler();
}
public ExpandableRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initRecycler();
}
private void initRecycler() {
setClipToPadding(false);
setItemAnimator(new DefaultItemAnimator());
}
@Override
public Parcelable onSaveInstanceState() {
//begin boilerplate code that allows parent classes to save state
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
//end
if (getAdapter() != null)
ss.stateToSave = ((Adapter) this.getAdapter()).getExpandedGroups();
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
//begin boilerplate code so parent classes can restore state
if (!(state instanceof SavedState)) // if state is not instance of out SaveState just restore in reg way
{
super.onRestoreInstanceState(state);
return;
}
// else if cast him to SavedState
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
//end
if (getAdapter() != null)
((Adapter) getAdapter()).setExpandedGroups(ss.stateToSave);
}
@Override
public void setAdapter(RecyclerView.Adapter adapter) {
if (!(adapter instanceof Adapter))
throw new IllegalArgumentException("adapter has to be of type ExpandableRecyclerView.Adapter");
super.setAdapter(adapter);
}
public interface OnChildItemClickedListener {
void onChildItemClicked(int group, int position);
}
static class SavedState implements Parcelable {
public static final SavedState EMPTY_STATE = new SavedState() {
};
//required field that makes Parcelables from a Parcel
public static final Creator<SavedState> CREATOR =
new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
SparseBooleanArray stateToSave;
Parcelable superState;
SavedState() {
superState = null;
}
SavedState(Parcelable superState) {
this.superState = superState != EMPTY_STATE ? superState : null;
}
private SavedState(Parcel in) {
Parcelable superState = in.readParcelable(ExpandableRecyclerView.class.getClassLoader());
this.superState = superState != null ? superState : EMPTY_STATE;
this.stateToSave = in.readSparseBooleanArray();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel out, int flags) {
out.writeParcelable(superState, flags);
out.writeSparseBooleanArray(this.stateToSave);
}
public Parcelable getSuperState() {
return superState;
}
}
public static abstract class Adapter<CVH extends ViewHolder, GVH extends ViewHolder, C, G> extends RecyclerView.Adapter<ViewHolder> {
private static final int TYPE_HEADER = 0;
SparseBooleanArray expanded = new SparseBooleanArray();
private OnChildItemClickedListener onChildItemClickedListener;
public Adapter() {
}
boolean isExpanded(int group) {
return expanded.get(group);
}
SparseBooleanArray getExpandedGroups() {
return expanded;
}
public void setExpandedGroups(SparseBooleanArray expanded) {
this.expanded = expanded;
}
public void expand(int group) {
if (isExpanded(group))
return;
// this lines of code calculate number of shown item in recycler view. also group is counting .
int position = 0;
for (int i = 0; i < group; i++) {
position++;
if (isExpanded(i))
position += getChildItemCount(i);
}
position++; // this for percent group
notifyItemRangeInserted(position, getChildItemCount(group)); // notify recycler view for expanding
expanded.put(group, true); // save expanding in sparce array
}
public void collapse(int group) {
if (!isExpanded(group)) // if is not expanded . so nothing to collapse.
return;
int position = 0;
for (int i = 0; i < group; i++) {
position++;
if (isExpanded(i))
position += getChildItemCount(i); // item
}
position++;
notifyItemRangeRemoved(position, getChildItemCount(group));
expanded.put(group, false);
}
public abstract int getGroupItemCount();
public abstract int getChildItemCount(int group);
@Override
public int getItemCount() {
int count = 0;
for (int i = 0; i <= getGroupItemCount(); i++) {
count += isExpanded(i) ? getChildItemCount(i) + 1 : 1;
}
return count;
}
public abstract G getGroupItem(int position);
public abstract C getChildItem(int group, int position);
public Object getItem(int i) {
int group = 0;
while (group <= getGroupItemCount()) {
if (i > 0 && !isExpanded(group)) {
i--;
group++;
continue;
}
if (i > 0 && isExpanded(group)) {
i--;
if (i < getChildItemCount(group))
return getChildItem(group, i);
i -= getChildItemCount(group);
group++;
continue;
}
if (i == 0)
return getGroupItem(group);
}
throw new IndexOutOfBoundsException();
}
@Override
public void onBindViewHolder(ViewHolder holder, int i) {
int group = 0;
while (group <= getGroupItemCount()) {
if (i > 0 && !isExpanded(group)) {
i--;
group++;
continue;
}
if (i > 0 && isExpanded(group)) {
i--;
if (i < getChildItemCount(group)) {
onBindChildViewHolder((CVH) holder, group, i);
return;
}
i -= getChildItemCount(group);
group++;
continue;
}
if (i == 0) {
onBindGroupViewHolder((GVH) holder, group);
return;
}
}
throw new IndexOutOfBoundsException();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return viewType == TYPE_HEADER ? onCreateGroupViewHolder(parent) : onCreateChildViewHolder(parent, viewType);
}
protected abstract GVH onCreateGroupViewHolder(ViewGroup parent);
protected abstract CVH onCreateChildViewHolder(ViewGroup parent, int viewType);
public abstract int getChildItemViewType(int group, int position);
@Override
public int getItemViewType(int i) {
int group = 0;
while (group <= getGroupItemCount()) {
if (i > 0 && !isExpanded(group)) {
i--;
group++;
continue;
}
if (i > 0 && isExpanded(group)) {
i--;
if (i < getChildItemCount(group))
return getChildItemViewType(group, i);
i -= getChildItemCount(group);
group++;
continue;
}
if (i == 0)
return TYPE_HEADER;
}
throw new IndexOutOfBoundsException();
}
public void setOnChildItemClickedListener(OnChildItemClickedListener onItemClickedListener) {
this.onChildItemClickedListener = onItemClickedListener;
}
public void onBindChildViewHolder(CVH holder, final int group, final int position) {
holder.itemView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (Adapter.this.onChildItemClickedListener != null) {
Adapter.this.onChildItemClickedListener.onChildItemClicked(group, position);
}
}
});
}
public void onBindGroupViewHolder(final GVH holder, final int group) {
if (holder instanceof GroupViewHolder)
((GroupViewHolder) holder).setExpanded(isExpanded(group));
holder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isExpanded(group)) {
collapse(group);
if (holder instanceof GroupViewHolder)
((GroupViewHolder) holder).collapse();
} else {
expand(group);
if (holder instanceof GroupViewHolder)
((GroupViewHolder) holder).expand();
}
}
});
}
}
public static abstract class GroupViewHolder extends ViewHolder {
public GroupViewHolder(View itemView) {
super(itemView);
}
public abstract void expand();
public abstract void collapse();
public abstract boolean isExpanded();
public abstract void setExpanded(boolean expanded);
}
public static class SimpleGroupViewHolder extends GroupViewHolder {
ImageView expandedIndicator;
TextView text;
private boolean expanded;
public SimpleGroupViewHolder(Context context) {
super(View.inflate(context, R.layout.group_header, null));
itemView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
expandedIndicator = (ImageView) itemView.findViewById(R.id.carbon_groupExpandedIndicator);
text = (TextView) itemView.findViewById(R.id.carbon_groupText);
}
public SimpleGroupViewHolder(Context context, int layout) {
super(View.inflate(context, layout, null));
itemView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
expandedIndicator = (ImageView) itemView.findViewById(R.id.carbon_groupExpandedIndicator);
text = (TextView) itemView.findViewById(R.id.carbon_groupText);
}
public void expand() {
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(200);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
expandedIndicator.postInvalidate();
}
});
animator.start();
expanded = true;
}
public void collapse() {
ValueAnimator animator = ValueAnimator.ofFloat(1, 0);
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(200);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
expandedIndicator.postInvalidate();
}
});
animator.start();
expanded = false;
}
@Override
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
ViewHelper.setRotation(expandedIndicator, expanded ? 180 : 0);
this.expanded = expanded;
}
public String getText() {
return text.getText().toString();
}
public void setText(String t) {
text.setText(t);
}
}
}
除了数据之外,适配器都是一样的,所以我举一个例子:
Adapter code example:
public class Adapter extends ExpandableRecyclerView.Adapter<Adapter.ChildViewHolder, Adapter.GroupViewHolder, String, String> {
private DatabaseHelper db;
private ArrayList List;
private int layout;
public Adapter(Context c, int resource) {
layout = resource;
db = new DatabaseHelper(c);
List = db.getRecords();//returns list
}
@Override
public int getGroupItemCount() {
return List.size() - 1;
}
@Override
public int getChildItemCount(int i) {
return 1;
}
@Override
public String getGroupItem(int i) {
return null;
}
@Override
public String getChildItem(int group, int child) {
return null;
}
@Override
protected GroupViewHolder onCreateGroupViewHolder(ViewGroup parent) {
return new GroupViewHolder(parent.getContext(), layout);
}
@Override
protected ChildViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_item, parent, false);
return new ChildViewHolder(rootView);
}
@Override
public void onBindGroupViewHolder(GroupViewHolder holder, int group) {
super.onBindGroupViewHolder(holder, group);
holder.setText1("A");
holder.setText2("B");
}
@Override
public void onBindChildViewHolder(ChildViewHolder holder, int group, int position) {
super.onBindChildViewHolder(holder, group, position);
holder.t1.setText("t1");
holder.t2.setText("t2");
holder.t3.setText("t3");
holder.t4.setText("t4");
}
@Override
public int getChildItemViewType(int i, int i1) {
return 1;
}
static class GroupViewHolder extends ExpandableRecyclerView.GroupViewHolder {
ImageView expandedIndicator;
TextView text1;
TextView text2;
private boolean expanded;
GroupViewHolder(Context context, int layout) {
super(View.inflate(context, layout, null));
itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
expandedIndicator = (ImageView) itemView.findViewById(R.id.groupExpandedIndicator);
text1 = (TextView) itemView.findViewById(R.id.text1);
text2 = (TextView) itemView.findViewById(R.id.text2);
}
public void expand() {
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(200);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
expandedIndicator.postInvalidate();
}
});
animator.start();
expanded = true;
}
public void collapse() {
ValueAnimator animator = ValueAnimator.ofFloat(1, 0);
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(200);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
expandedIndicator.postInvalidate();
}
});
animator.start();
expanded = false;
}
@Override
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
ViewHelper.setRotation(expandedIndicator, expanded ? 180 : 0);
this.expanded = expanded;
}
void setText1(String t) {
text1.setText(t);
}
void setText2(String t) {
text2.setText(t);
}
}
class ChildViewHolder extends RecyclerView.ViewHolder {
private TextView t1;
private TextView t2;
private TextView t3;
private TextView t4;
ChildViewHolder(View itemView) {
super(itemView);
t1 = (TextView) itemView.findViewById(R.id.tv1);
t2 = (TextView) itemView.findViewById(R.id.tv2);
t3 = (TextView) itemView.findViewById(R.id.tv3);
t4 = (TextView) itemView.findViewById(R.id.tv4);
}
}
}
group_header.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/Card1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="10dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="@+id/text1"
style="@style/MyLargeTextAppearance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="16dp"
android:textColor="@color/highlight"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Card1"
android:layout_gravity="center"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="1dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="@+id/text2"
style="@style/MyLargeTextAppearance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="16dp"
android:textColor="@color/highlight"
android:textStyle="bold" />
<ImageView
android:id="@+id/groupExpandedIndicator"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:srcCompat="@drawable/ic_expand_more" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
group_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="10dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/materialGray"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/t1"
style="@style/MyLargeTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/separatorViewBackground" />
<TextView
android:id="@+id/t2"
style="@style/MyLargeTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/separatorViewBackground" />
<TextView
android:id="@+id/t3"
style="@style/MyLargeTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/separatorViewBackground" />
<TextView
android:id="@+id/t4"
style="@style/MyLargeTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
</android.support.v7.widget.CardView>
最佳答案
尝试更改为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.rv, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new a_Adapter(getContext(), R.layout.group_header));
return recyclerView;
}
关于android:指定的 child 已经有一个 parent 。你必须调用 removeView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986468/
如果我需要选择第 10 个父级,是否有更简洁的方法,然后重复 .parent() 10 次? $('#element_id').parent().parent().parent().parent().
从 angularJS 指南中的“如何创建通信指令”开始,https://docs.angularjs.org/guide/directive , 我正在尝试使用该布局来制作可导航的表单。 问题在于指
我有一个 jQuery 函数,需要获取元素父元素的位置。 它看起来像: function show(e) { //debugger; var nextTab
我正在尝试修复这个难看的代码。 RadGrid gv = (RadGrid) (((Control) e.CommandSource).Parent.Parent.Parent.Parent.Pare
我有一个 A 标签,可以触发它的曾曾曾祖 parent 的动画。以下所有方法都可以,但哪一个最有效,为什么? $(this).parent().parent().parent().parent().p
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在尝试定位绝对定位的 div 时遇到了一些问题。我猜它的工作方式应该是这样,但是我希望它与父对象的父对象而不是父对象一起使用,因为我有一个下拉列表,当我希望它像第一个一样保持在顶部时,它会跟随父对象
我正在做一些非常基本的 jQuery 东西,真正开始,我经常通过做类似的事情来向上导航 dom $(this).parent().parent().addClass('hello'); 我只是想知道是
此 HTML 结构有一个 div#page,其中当前页面内容将通过 Ajax 加载。内容始终由 section 标记组成,这些标记可以具有动态高度(相对于浏览器的百分比)或静态高度(以像素为单位)。
在 javascript 中是否有一种简单的方法来定位父对象的父对象? 我使用 this.parentNode 作为函数的元素来选择父节点,我尝试了 this.parent.parentNode 和
当遍历 pager.Pages 对象的 foreach 循环时,$data 是 self(正如预期的那样)。但是,$parent 应该是寻呼机对象,但它返回的是 WaterQualityResultV
在架构中,我想根据父级的 sibling 调整架构。 例如:如果 toggleMonday 为真,那么 weekdays -> monday 应该有一个特定的验证模式。 现在下面的例子有效。但是,它非
我想要完成的是,当用户将焦点放在文本框上时,其中的字段集将添加一个类“active_fieldset”,以便提供用户在表单中的位置的良好视觉提示。使用以下 javascript,它确实会影响父字段集,
我创建了这个函数来保存我的taches sauverTache(tache:Tache){ this.editionEnCours = true; tache.estReelle =
所以..这是我的问题..我有以下代码(示例): var GameObject = function (posX, posY, width, height) { this.posX = posX;
所以,我是 jQuery 的新手,我正在尝试更改关于函数触发器的 2 个级别的 div: 这是我的第一次尝试:我尝试找到最接近的“.node”,它是所有其他 div 的父级并编辑子 div。 fun
我想了解为什么使用 ng-repeat在repeat 的item 上有某个controller,那个item 的parent 和那个item 的祖父是同一个controller。我期待祖父成为父 Co
我想从我的组件 Controller 之一将 jsonModel 设置为我的 SAPUI5 组件。在组件内,我使用应用程序或 splitapp。 我想避免通过 ID 获取元素。从组件内的某个位置获取层
我不确定如何在标题上准确地表达出来,因为问题在我的场景中太具体了,但无论如何基本上我有两个类似于下面的外部类: class Config { public level: number = 1;
在我正在编写的这个脚本中,我发现自己连续使用 .parent() 最多七次来获取元素。虽然这有效,但似乎可以/应该有一种更简单的方法来完成我不知道的这个/功能。除了更多元素上更具体的类/ID 之外,还
我是一名优秀的程序员,十分优秀!