- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个小型应用程序,用户可以在其中发布内容和对帖子发表评论。我正在使用 github 上的 Instamaterial 项目作为引用。
当用户发布新评论时,会出现带有动画的评论 Activity 。一切都很顺利......但是当我发布第一条评论时......应用程序崩溃并且没有显示评论动画(滚动到顶部)我花了一整天的时间尝试......但找不到解决方案。
public class CommentsActivity extends AppCompatActivity implements SendCommentButton.OnSendClickListener {
public static final String ARG_DRAWING_START_LOCATION = "arg_drawing_start_location";
LinearLayout contentRoot;
RecyclerView rvComments;
LinearLayout llAddComment;
EditText etComment;
SendCommentButton btnSendComment;
private CommentsAdapter commentsAdapter;
private int drawingStartLocation;
Toolbar toolbar;
private int currentPostID;
DatabaseHelper db;
private PreferencesManager preferencesManager;
ArrayList<PostResponse.PostComments> comments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
toolbar = (Toolbar) findViewById(R.id.toolbar);
contentRoot = (LinearLayout) findViewById(R.id.contentRoot);
rvComments = (RecyclerView) findViewById(R.id.rvComments);
llAddComment = (LinearLayout) findViewById(R.id.llAddComment);
etComment = (EditText) findViewById(R.id.etComment);
btnSendComment = (SendCommentButton) findViewById(R.id.btnSendComment);
preferencesManager = new PreferencesManager(CommentsActivity.this);
db = new DatabaseHelper(CommentsActivity.this);
setupToolbar();
setupComments();
setupSendCommentButton();
drawingStartLocation = getIntent().getIntExtra(ARG_DRAWING_START_LOCATION, 0);
if (savedInstanceState == null) {
contentRoot.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
contentRoot.getViewTreeObserver().removeOnPreDrawListener(this);
startIntroAnimation();
return true;
}
});
}
}
protected void setupToolbar() {
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShow
HomeEnabled(true);
toolbar.setNavigationIcon(R.mipmap.ic_menu_cancel);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
private void setupComments() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rvComments.setLayoutManager(linearLayoutManager);
rvComments.setHasFixedSize(true);
currentPostID = getIntent().getIntExtra(PostResponse.KEY_ID, -1);
comments = db.getCommentsByPostID(currentPostID);
commentsAdapter = new CommentsAdapter(this, comments);
rvComments.setAdapter(commentsAdapter);
rvComments.setOverScrollMode(View.OVER_SCROLL_NEVER);
rvComments.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
commentsAdapter.setAnimationsLocked(true);
}
}
});
}
private void setupSendCommentButton() {
btnSendComment.setOnSendClickListener(this);
}
private void startIntroAnimation() {
ViewCompat.setElevation(getToolbar(), 0);
contentRoot.setScaleY(0.1f);
contentRoot.setPivotY(drawingStartLocation);
llAddComment.setTranslationY(200);
contentRoot.animate()
.scaleY(1)
.setDuration(200)
.setInterpolator(new AccelerateInterpolator())
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewCompat.setElevation(getToolbar(), Utils.dpToPx(8));
animateContent();
}
})
.start();
}
private void animateContent() {
commentsAdapter.updateItems();
llAddComment.animate().translationY(0)
.setInterpolator(new DecelerateInterpolator())
.setDuration(200)
.start();
}
@Override
public void onBackPressed() {
ViewCompat.setElevation(getToolbar(), 0);
contentRoot.postDelayed(new Runnable() {
@Override
public void run() {
contentRoot.animate()
.translationY(Utils.getScreenHeight(CommentsActivity.this))
.setDuration(200)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
CommentsActivity.super.onBackPressed();
overridePendingTransition(0, 0);
}
})
.start();
}
}, 100);
}
@Override
public void onSendClickListener(View v) {
if (validateComment()) {
publishComment(etComment.getText().toString());
PostResponse.PostComments newComment = new PostResponse().new PostComments();
newComment.user_id = Integer.parseInt(preferencesManager.getUserID());
newComment.comment = etComment.getText().toString();
newComment.post_id = currentPostID;
commentsAdapter.addItem(newComment);
commentsAdapter.setAnimationsLocked(false);
commentsAdapter.setDelayEnterAnimation(true);
rvComments.smoothScrollBy(0, rvComments.getChildAt(0).getHeight() * commentsAdapter.getItemCount());
etComment.setText(null);
btnSendComment.setCurrentState(SendCommentButton.STATE_DONE);
}
}
private boolean validateComment() {
if (TextUtils.isEmpty(etComment.getText())) {
btnSendComment.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake_error));
return false;
}
return true;
}
private Toolbar getToolbar(){
return toolbar;
}
/************************************************
* VOLLEY CALLS TO PUBLISH A COMMENT
* *****************************************************/
private void publishComment(final String comment) {
//Code here
}
}
这是我的适配器
public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private int lastAnimatedPosition = -1;
private int avatarSize;
private int itemsCount = 0;
private boolean animationsLocked = false;
private boolean delayEnterAnimation = true;
List<PostResponse.PostComments> mItems;
public CommentsAdapter(Context context, List<PostResponse.PostComments> mItems) {
this.context = context;
this.mItems = mItems;
avatarSize = context.getResources().getDimensionPixelSize(R.dimen.comment_avatar_size);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
return new CommentViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
runEnterAnimation(viewHolder.itemView, position);
CommentViewHolder holder = (CommentViewHolder) viewHolder;
holder.tvComment.setText(mItems.get(position).comment);
Picasso.with(context)
.load(R.drawable.user_profile)
.centerCrop()
.resize(avatarSize, avatarSize)
.transform(new RoundedTransformation())
.into(holder.ivUserAvatar);
}
private void runEnterAnimation(View view, int position) {
if (animationsLocked) return;
if (position > lastAnimatedPosition) {
lastAnimatedPosition = position;
view.setTranslationY(100);
view.setAlpha(0.f);
view.animate()
.translationY(0).alpha(1.f)
.setStartDelay(delayEnterAnimation ? 20 * (position) : 0)
.setInterpolator(new DecelerateInterpolator(2.f))
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animationsLocked = true;
}
})
.start();
}
}
@Override
public int getItemCount() {
return mItems.size();
}
public void updateItems() {
notifyDataSetChanged();
}
public void addItem(PostResponse.PostComments newComment) {
mItems.add(newComment);
notifyItemInserted(mItems.size() - 1);
}
public void setAnimationsLocked(boolean animationsLocked) {
this.animationsLocked = animationsLocked;
}
public void setDelayEnterAnimation(boolean delayEnterAnimation) {
this.delayEnterAnimation = delayEnterAnimation;
}
public static class CommentViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.ivUserAvatar)
ImageView ivUserAvatar;
@Bind(R.id.tvComment)
TextView tvComment;
public CommentViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
这是屏幕。
第一个效果很好,但添加第一个评论永远不会奏效。我得到这个异常(exception)。
//这里是异常(exception)
java.lang.NullPointerException
at com.test.abc.CommentsActivity.onSendClickListener(CommentsActivity.java:214)
at com.test.abc.views.SendCommentButton.onClick(SendCommentButton.java:80)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
最佳答案
我知道你知道这是因为这条线 - (你在评论中告诉我)
rvComments.smoothScrollBy(0, rvComments.getChildAt(0).getHeight() *
commentsAdapter.getItemCount());
但你知道这是方法调用吗? rvComments.getChildAt(0).getHeight()
?
我的想法是,如果 recyleView 为空调用 rvComments.getChildAt(0)
是对 Null Reference 的引用,它是空的因此你 NPE
但如果第一条评论已经添加,你有大小为 1 的列表,所以这一行 rvComments.getChildAt(0).getHeight()
实际上引用了一个 View 对象地址.
所以我猜你的解决方案是_因为你知道它的首选高度 查看或选择一个首选高度并对它进行硬编码并使用。或者,如果您真的坚持使用 View 高度,请在继续之前检查列表大小。
...
commentsAdapter.setAnimationsLocked(false);
commentsAdapter.setDelayEnterAnimation(true);
if(rvComments.getChildCount() > 0){
//i guess you will have do rvComments.getChildAt(
//rvComments.getChildCount()-1) rather than below
rvComments.smoothScrollBy(0, rvComments.getChildAt(0).getHeight()
* commentsAdapter.getItemCount());
}else{
//do something else or find something to do to get the height you want
//and multiply. In short it means the first item.
...
关于android - 添加第一个项目时 Recyclerview 项目动画崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35906619/
我创建了一个用户可以添加测试的字段。这一切运行顺利我只希望当用户点击(添加另一个测试)然后上一个(添加另一个测试)删除并且这个显示在新字段中。 所有运行良好的唯一问题是点击(添加另一个字段)之前添加另
String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在努力将 jQuery 滚动功能添加到 nav-tab (Bootstrap 3)。我希望用户能够选择他们想要的选项卡,并在选项卡内容中有一个可以平滑滚动到 anchor 的链接。这是我的代码,可
我正在尝试在用户登录后再添加 2 个 ui 选项卡。首先,我尝试做一个之后。 $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
我有一个包含选择元素的表单,我想通过选择添加和删除其中一些元素。这是html代码(这里也有jsfiddle http://jsfiddle.net/txhajy2w/):
正在写这个: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) 等同于: view.backgroundColor = UICo
好的,如果其中有任何信息,我想将这些列添加到一起。所以说我有 账户 1 2 3 . 有 4 个帐户空间,但只有 3 个帐户。我如何创建 java 脚本来添加它。 最佳答案 Live Example H
我想知道是否有一种有效的预制算法来确定一组数字的和/差是否可以等于不同的数字。示例: 5、8、10、2,使用 + 或 - 等于 9。5 - 8 = -3 + 10 = 7 + 2 = 9 如果有一个预
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我的 Oracle SQL 查询如下- Q1- select hca.account_number, hca.attribute3, SUM(rcl.extended_amou
我正在阅读 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
我正在尝试添加一个“加载更多”按钮并限制下面的结果,这样投资组合页面中就不会同时加载 1000 个内容,如下所示:http://typesetdesign.com/portfolio/ 我对 PHP
我遇到这个问题,我添加了 8 个文本框,它工作正常,但是当我添加更多文本框(如 16 个文本框)时,它不会添加最后一个文本框。有人遇到过这个问题吗?提前致谢。 Live Link: JAVASCRIP
add/remove clone first row default not delete 添加/删除克隆第一行默认不删除&并获取正确的SrNo(例如:添加3行并在看到问题后删除SrNo.2)
我编码this ,但删除按钮不起作用。我在控制台中没有任何错误.. var counter = 0; var dataList = document.getElementById('materi
我有一个类似数组的对象: [1:数组[10]、2:数组[2]、3:数组[2]、4:数组[2]、5:数组[3]、6:数组[1]] 我正在尝试删除前两个元素,执行一些操作,然后将它们再次插入到同一位置。
使用的 Delphi 版本:2007 你好, 我有一个 Tecord 数组 TInfo = Record Name : String; Price : Integer; end; var Info
我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件) 我的js代
title 323 323 323 title 323 323 323 title 323 323 323 JS $(document).keydown(function(e){
我是一名优秀的程序员,十分优秀!